/**
* @function ajax request
* @fields ajaxName:请求名称,method:请求方法,headers:setRequestHeader自定义部分,url:接口地址,async:是否异步请求,withCredentials:是否支持跨域发送cookie,dataType:数据类型 ,data:post请求参数
* @param data:{ajaxName:"ajaxNameString",headers:{},method:"GET/POST",url:"",async:true/false,withCredentials:true/false,dataType:"json",data:""}
* @result ajaxName.responseText
*/
function ajaxRequest(data, callback) {
data = data || {};
data.dataType = data.dataType || 'json';
var sendParams = null;
var headers = data.headers || {};
var ajax = data.ajaxName;
// 新建请求
if (window.XMLHttpRequest) {
ajax = new XMLHttpRequest();
} else {
ajax = new ActiveXObject('Microsoft.XMLHTTP');
}
// 打开请求
ajax.open(data.method.toUpperCase(), data.url, data.async);
// 是否支持跨域发送cookie
ajax.withCredentials = data.withCredentials;
// POST请求设置
if (data.method == 'POST') {
ajax.setRequestHeader("Content-type", data.contentType || "application/x-www-form-urlencoded");
for (var i in headers) {
ajax.setRequestHeader(i, headers[i]);
}
if (data.contentType) {
sendParams = data.data;
}
}
// 发送请求
ajax.send(sendParams ? sendParams : null);
// 注册事件
ajax.onreadystatechange = function () {
callback(ajax);
}
}
调用方法:
ajaxRequest({
ajaxName: ajaxName,
headers: {
"x-access-ep": "wap"
},
contentType: "application/json;charset=utf-8",
method: "POST",
url: requestUrl,
async: true,
withCredentials: true,
dataType: "json",
data: JSON.stringify(params[item])
}, function callback(ajax) {
if(ajax.status == 200 && ajax.readyState == 4){
console.log(ajax);
}
})