前端之家收集整理的这篇文章主要介绍了
XmlHttpReques,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
function ajaxRequest(url,data,method,fn) {
var request = new XMLHttpRequest();
request.open(method,url,true);
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200)
fn(eval('(' + request.responseText + ')'));
else if (request.readyState == 4 && request.status == 500) {
console.log(request.responseText);
alert("未知错误");
return false;
}
}
var params = "";
if (typeof data == 'object') {
for (var d in data)
params += d + "=" + data[d] + "&";
params = params.substr(0,params.length - 1);
}
else
params = data;
request.send(params);
}