javascript发送get、post http请求

1. 获得XMLHttpRequest对象

function createXMLHttpRequest() {
	var xmlHttp;
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
		if (xmlHttp.overrideMimeType)
			xmlHttp.overrideMimeType('text/xml');
	} else if (window.ActiveXObject) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
	return xmlHttp;
}

2. 发送get请求
    xmlHttp = createXMLHttpRequest();
    var url = "getfiledetail.jsp?fileid="+id;
    xmlHttp.open("GET", url, true);// 异步处理返回 
    xmlHttp.onreadystatechange = callback; 
    xmlHttp.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded;");
    xmlHttp.send();


3. 发送post请求

    var url = "getNginxStatus";
    xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange = getStatusBack;
    xmlHttp.setRequestHeader("Content-Type",
	   "application/x-www-form-urlencoded;");
    xmlHttp.send(xml);

ps.额外小功能

1.  网页显示xml的标签:<xmp></xmp>

2.  input标签onclick传参:

οnclick='onclickfunction("String1","String2");'

不过传的是常量……


发布了35 篇原创文章 · 获赞 1 · 访问量 11万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章