function AJAX() {
var xhr = null;
if(window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
try{
xhr = new ActiveXObject('Microsoft.XMLHttp');
}catch(e){
xhr = new ActiveXObject('msxml2.xmlhttp');
}
}
//get请求
this.get = function(url,success,fail){
xhr.open("GET", "1.jsp",true);
xhr.onreadystatechange=function(){
if(xhr.readyState==4) {
alert(xhr.status);
if(xhr.status==200) {
var txt = xhr.responseText;
txt = eval("("+txt+")");
var ch = txt.charAt(0);
if(ch=="
var xml = xhr.responseXML;
success(eval("("+xml+")"));//【慎重使用eval】
} else if(ch=="["||ch=="{") {//json类型
txt = eval("("+txt+")");//【慎重使用eval】
success(txt);
} else {
success(txt);
}
} else {
if(fail) {
fail(xhr.status);
}
}
}
};
xhr.send(null);
};
//post请求
this.post = function (url,param,success,fail) {
xhr.open("POST", "1.jsp",true);
xhr.onreadystatechange=function(){
if(xhr.readyState==4) {
alert(xhr.status);
if(xhr.status==200) {
var txt = xhr.responseText;
var ch = txt.charAt(0);
if(ch=="
var xml = xhr.responseXML;
success(eval("("+xml+")"));//【慎重使用eval】
} else if(ch=="["||ch=="{") {//json类型
txt = eval("("+txt+")");//【慎重使用eval】
success(txt);
} else {
success(txt);
}
} else {
if(fail) {
fail(xhr.status);
}
}
}
};
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(param);
};
}
*【注】:脚本中返回的数据结果,eval的使用需要慎重,在IE中可能会出现内存问题。