(function(){
var Http = {//ajax操作
xhr : false,
xhrs : [
function () {return new XMLHttpRequest();},
function () {return new ActiveXObject("Msxml2.XMLHTTP");},
function () {return new ActiveXObject("Microsoft.XMLHTTP")}
],
getXhr : function() {
for(var i = 0; i < Http.xhrs.length; i++){
try{
this.xhr = Http.xhrs[i]();
if(this.xhr != null){
return this.xhr;
}
}catch(e){
}
}
if(this.xhr == false){alert("不支持AJAX");}
},
callback : function(callback){
return function (){
if(this.readyState == 4){
if(this.status == 200){
callback.s(this);
}else{
callback.f() || null;
}
}
}
},
G : function(url, callback, data) {
var xhr = Http.getXhr();
var url = data ? url + "?" + data + "&time=" + new Date().getTime() : url + "?time=" + new Date().getTime();
if(xhr){
xhr.onreadystatechange = this.callback(callback);
xhr.open("GET", url, true);
xhr.send(null);
}
},
P : function(url, callback, data) {
var xhr = Http.getXhr();
var url = url + "?time=" + new Date().getTime();
if(xhr){
xhr.onreadystatechange = this.callback(callback);
xhr.open("POST", url, true);
xhr.send(encodeURIComponent(data));//decodeURIComponent 对应解码
}
}
}
})()
经过初步测试无错误,如有发现错误请留言~第一时间改正
Ajax封装
最新推荐文章于 2024-09-17 14:30:35 发布