function ajax(method, url, callback, data, flag) {
var xhr = null;
if(window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}else {
xhr = new ActiveXObject('Microsoft.XMLHttp')
}
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
callback(xhr.responseText);
}else {
console.log('error');
}
}
}
method = method.toUpperCase();
if(method == 'GET') {
var date = new Date(),
timer = date.getTime();
xhr.open(method, url + '?' + data + '&timer=' + timer, flag);
xhr.send();
}else if(method == 'POST') {
xhr.open(method, url, flag);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send(data);
}
}
js封装ajax
最新推荐文章于 2024-06-26 15:10:02 发布