用的原生XMLHttpRequest,发现xhr.send(data)请求超时以后不会报错
修改了一下代码,设置send请求超时时间,获取xhr.send()异常
new Promise(function(resolve,reject){
//原生构造ajax请求
let xhr = new XMLHttpRequest();
//设置请求超时处理
xhr.ontimeout = function () {
console.error(" timed out");
};
xhr.responseType = 'json';
//post请求需要的参数
let data = new FormData();
data.append('length', length);
//post需要设置请求头
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset-UTF-8");
//请求响应
xhr.onload = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(this.response)
} else {
console.error(xhr.statusText);
}
}
};
xhr.open('post','/fa/b');
//设置请求超时的时间
xhr.timeout = 2000;
//发送请求
xhr.send(data);
}).then((res)=>{
console.log(res)
},
(err)=>{
console.log(err);
});
网友评论