在前端调用封装的ajax,往服务器发送请求时出现Uncaught (in promise),method为get,封装ajax代码如下:
function ajax(method,url,postStr,dateType="json"){
return new Promise((resolve,reject)=>{
var xhr = (function(){
if(window.XMLHttpRequest){
return new XMLHttpRequest();
}else{
return new ActiveXObject("Microsoft.XMLHttp");
}
})();
xhr.open(method,url,true);
if(method == 'post'){
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded")
}
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
if(url.indexOf(".php")!=-1 && dateType.toLowerCase()=="json"){
console.log(xhr.responseText);
resolve(JSON.parse(xhr.responseText));
}else{
console.log(xhr.responseText);
resolve(xhr.responseText);
}
}else
reject("ajax出错啦!"+xhr.readyState + xhr.status);
}
xhr.send(postStr);
})
}