封装的很NB的ajax函数,羡慕中.....
1.判断浏览器支持操作xml操作类型
function bg_Ajax_GetXMLHttpRequest() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else {
if (window.Ajax_XMLHttpRequestProgID) {
return new ActiveXObject(window.Ajax_XMLHttpRequestProgID);
} else {
var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
for (var i = 0; i < progIDs.length; ++i) {
var progID = progIDs[i];
try {
var x = new ActiveXObject(progID);
window.Ajax_XMLHttpRequestProgID = progID;
return x;
} catch (e) {
}
}
}
}
return null;
}
2.回调函数
function bg_Ajax_Callback(url, postData, callback)
{
var x = bg_Ajax_GetXMLHttpRequest();
var result = null;
if (!x) {
result = { "value":null, "error": "NOXMLHTTP"};
if (debugErrors) {
alert("error: " + result.error);
}
if (clientCallBack) {
clientCallBack(result);
}
return result;
}
x.open("POST", url, callback ? true : false);
x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
if (callback) {
x.onreadystatechange = function() {
var result = null;
if (x.readyState != 4) {
return;
}
callback(x.responseText)
}
}
x.send(postData);
}