And*_*nov 5 javascript xmlhttprequest fetch
如何检索有关网络错误的确切信息?Chrome 浏览器将诸如“net::ERR_CONNECTION_TIMED_OUT”或“net::ERR_CERT_INVALID”之类的内容打印到控制台中,但如何通过 JS 检索它们?有可能得到它们吗?目前我使用 fetch API,但我可以轻松切换到 XMLHTTPRequest。
我需要它来处理此类错误以通知用户并让他知道他需要做什么。
小智 -3
您可以得到确切的错误,如下所示:
fetch('url',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify({data})
})
.then(function(response) {
console.log(response);
//handle here or in catch
})
.then(function(json){
console.log("succeed json re");
});
Run Code Online (Sandbox Code Playgroud)
一旦您的承诺得到解决,您就会得到解决的响应。
Response.status将为您提供状态代码。 Response.statusText将为您提供准确的错误描述,如 ERR_CONNECTION_TIMED_OUT。如果出现错误,response.ok将为 false。
欲了解更多信息:FETCH
| 归档时间: |
|
| 查看次数: |
960 次 |
| 最近记录: |