<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 如何获取响应体?

      Fetch API how to get response body in catch?(Fetch API 如何获取响应体?)

      <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 如何获取响应体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass
                在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                Ordinals in words javascript(javascript中的序数)

                        <tbody id='tPtOa'></tbody>
                      <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>