长知识了
ajax_remote: function(url,method,async,params,callback){
var result=false;
if(method== undefined)
method = "post";
if(callback == undefined)
callback=false;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
console.log("-----xhr.readyState----" +xhr.readyState);
console.log("-----xhr.status----" +xhr.status);
if (xhr.readyState === 4) {
if(xhr.status===200){
var data = xhr.responseText;
console.log('-------xhr responseText-----:',data);
data=JSON.parse(data);
//callback(data);
callback && callback.call(this, data);
result=data;
}else if(xhr.status===404){
console.log('------response 404------');
callback && callback.call(this, null);
result=false;
}else{
console.log('------response------',xhr.status);
callback && callback.call(this, null);
result=false;
}
}else{
console.log('-----xhr调用服务器出错或者其它状态readyState,status-----',xhr.readyState,xhr.status);
result=false;
}
};
xhr.open(method, url, async);//true同步,false异步
(method=='POST' || method=='post')&&(xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"));
xhr.timeout = 5000; // 超时时间,单位是毫秒
xhr.onload = function () {
// 请求完成。在此进行处理。
console.log('----------xhr onload----------');
};
xhr.ontimeout = function (e) {
// XMLHttpRequest 超时。在此做某事。
console.log('----------xhr ontimeout----------',e);
callback && callback.call(this,null);
result=false;
};
xhr.onerror = function (e) {
// XMLHttpRequest 超时。在此做某事。
console.log('----------xhr onerror----------',e);
callback && callback.call(this,null);
result=false;
};
xhr.send(params);
return result;
},