[英]Fetch API how to get response body in catch?
I got the response from server like this: 我从服务器收到这样的响应:
Request URL:http://api.bfexchange.net/wallet/balance
Request Method:GET
Status Code:504 Gateway Time-out
Remote Address:52.78.140.154:80
Referrer Policy:no-referrer-when-downgrade
But printing err in catch of fetch API just returns Error object. 但是在获取API时打印err只会返回Error对象。
fetch(...)
.then(...)
.catch((err) => {
console.dir(err);
});
It prints this(from Google Chrome): 它打印此(从谷歌浏览器):
TypeError: Failed to fetch
message: "Failed to fetch"
stack: "TypeError: Failed to fetch"
__proto__: Error
index.js:82
I want to get status code to proper error handling, like server is not responding like something. 我想获取状态代码以进行正确的错误处理,例如服务器未响应。
Is there a way to get response data in this case? 在这种情况下,有没有办法获取响应数据? Else, what alternatives can I attempt? 否则,我可以尝试哪些替代方法?
Any advice will very appreciate it! 任何建议将不胜感激!
If the server uses CORS you will get the 5xx response, but it won't be as a rejection; 如果服务器使用CORS,您将收到5xx响应,但这不会被拒绝; the promise will be resolved with the response. 承诺将通过响应得到解决。 If the server does not use CORS, you won't ever see a response and the promise will always be rejected (unless you use "no-cors", in which case you can get an opaque response that is useful in some service worker scenarios). 如果服务器不使用CORS,您将永远不会看到响应,并且承诺将始终被拒绝(除非您使用“ no-cors”,在这种情况下,您会得到不透明的响应,在某些服务工作者场景中很有用) )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.