<tfoot id='IiD51'></tfoot>
    • <bdo id='IiD51'></bdo><ul id='IiD51'></ul>

    <small id='IiD51'></small><noframes id='IiD51'>

      <legend id='IiD51'><style id='IiD51'><dir id='IiD51'><q id='IiD51'></q></dir></style></legend>
    1. <i id='IiD51'><tr id='IiD51'><dt id='IiD51'><q id='IiD51'><span id='IiD51'><b id='IiD51'><form id='IiD51'><ins id='IiD51'></ins><ul id='IiD51'></ul><sub id='IiD51'></sub></form><legend id='IiD51'></legend><bdo id='IiD51'><pre id='IiD51'><center id='IiD51'></center></pre></bdo></b><th id='IiD51'></th></span></q></dt></tr></i><div id='IiD51'><tfoot id='IiD51'></tfoot><dl id='IiD51'><fieldset id='IiD51'></fieldset></dl></div>

      Fetch API 如何获取响应体?

      时间:2023-10-01

      <legend id='JGuw5'><style id='JGuw5'><dir id='JGuw5'><q id='JGuw5'></q></dir></style></legend>

      <tfoot id='JGuw5'></tfoot>
      1. <small id='JGuw5'></small><noframes id='JGuw5'>

          <bdo id='JGuw5'></bdo><ul id='JGuw5'></ul>
          <i id='JGuw5'><tr id='JGuw5'><dt id='JGuw5'><q id='JGuw5'><span id='JGuw5'><b id='JGuw5'><form id='JGuw5'><ins id='JGuw5'></ins><ul id='JGuw5'></ul><sub id='JGuw5'></sub></form><legend id='JGuw5'></legend><bdo id='JGuw5'><pre id='JGuw5'><center id='JGuw5'></center></pre></bdo></b><th id='JGuw5'></th></span></q></dt></tr></i><div id='JGuw5'><tfoot id='JGuw5'></tfoot><dl id='JGuw5'><fieldset id='JGuw5'></fieldset></dl></div>
            <tbody id='JGuw5'></tbody>

                本文介绍了Fetch API 如何获取响应体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我从服务器收到这样的响应:

                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
                

                但是在 fetch API 的 catch 中打印 err 只会返回 Error 对象.

                But printing err in catch of fetch API just returns Error object.

                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!

                推荐答案

                如果服务器使用CORS,你会得到5xx响应,但不会作为拒绝;承诺将通过响应得到解决.如果服务器不使用 CORS,您将永远看不到响应,并且承诺将始终被拒绝(除非您使用no-cors",在这种情况下,您可以获得在某些服务工作者场景中有用的不透明响应).

                If the server uses CORS you will get the 5xx response, but it won't be as a rejection; 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).

                这篇关于Fetch API 如何获取响应体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:每 2 秒获取一次调用,但不希望请求堆积 下一篇:JavaScript Fetch:有编码问题的字符

                相关文章

                    <legend id='tPtOa'><style id='tPtOa'><dir id='tPtOa'><q id='tPtOa'></q></dir></style></legend>
                  1. <i id='tPtOa'><tr id='tPtOa'><dt id='tPtOa'><q id='tPtOa'><span id='tPtOa'><b id='tPtOa'><form id='tPtOa'><ins id='tPtOa'></ins><ul id='tPtOa'></ul><sub id='tPtOa'></sub></form><legend id='tPtOa'></legend><bdo id='tPtOa'><pre id='tPtOa'><center id='tPtOa'></center></pre></bdo></b><th id='tPtOa'></th></span></q></dt></tr></i><div id='tPtOa'><tfoot id='tPtOa'></tfoot><dl id='tPtOa'><fieldset id='tPtOa'></fieldset></dl></div>
                    <tfoot id='tPtOa'></tfoot>

                    <small id='tPtOa'></small><noframes id='tPtOa'>

                      <bdo id='tPtOa'></bdo><ul id='tPtOa'></ul>