自已封装Ajax方法

function createXHR() {
var request;
if (typeof (XMLHttpRequest) == 'undefined') {
request = new ActiveXObject('Microsoft.XMLHTTP');
} else {
request = new XMLHttpRequest();
}
return request;
} var xhr = createXHR(); function ajax(method, url, isAsync, data, fnsuccess, fnerror) {
xhr.open(method, url, isAsync);
if (method.toLocaleLowerCase() == 'post') {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
return fnsuccess(xhr.responseText);
} else {
return fnerror();
}
}
};
xhr.send(data);
} function get(url, fnsuccess) {
return ajax("get", url, true, null, fnsuccess, null);
} function post(url, data, fnsuccess) {
return ajax("post", url, true, data, fnsuccess, null);
}
上一篇:封装ajax方法


下一篇:Windows 10开发基础——VS2015 Update1新建UWP项目,XAML设计器无法加载的解决