树青兄写的一个ajax类。
var http_request = false;
function send_request(method,url,formName,responseType,callback) {
http_request = false;
var content =""
if(forName != null)content = getFormAsString(formName);
if(window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
if(responseType.toLowerCase()=="text") {
http_request.onreadystatechange = callback;
}
else if(responseType.toLowerCase()=="xml") {
http_request.onreadystatechange = callback;
}
else {
window.alert("响应类别参数错误。");
return false;
}
if(method.toLowerCase()=="get") {
http_request.open(method, url, true);
}
else if(method.toLowerCase()=="post") {
http_request.open(method, url, true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
else {
window.alert("http请求类别参数错误。");
return false;
}
if(window.ActiveXObject) http_request.setRequestHeader("If-Modified-Since", "0");
http_request.send(content);
}
function getFormAsString(formName){
returnString ="";
formElements=document.forms[formName].elements;
for(var i=formElements.length-1;i>=0; --i ){
returnString+="&"
+escape(formElements[i].name)+"="
+escape(formElements[i].value);
}
}