xhr 对象如何捕捉链接错误
1个回答
展开全部
xhr 对象如何捕捉链接错误
function getUrl(url) {
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
console.log(req);
req.open('GET', url, true);
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status === 200 || req.status === 304) {
resolve(req.responseText);
}
} else {
reject(new Error(req.statusText));//发现onerror没有触发后而加上的
}
}
req.onerror = function () {
reject(new Error(req.statusText));
}
req.send();
})
}
getUrl('./index.html').then(function (text) {
console.log(text);
}).catch(function (e) {
console.log(e);
})
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询