1. <legend id='mpTT9'><style id='mpTT9'><dir id='mpTT9'><q id='mpTT9'></q></dir></style></legend>

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

      1. <tfoot id='mpTT9'></tfoot>

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

        如何确定 Fetch 请求的状态码?

        时间:2023-10-01
        <tfoot id='QSnVW'></tfoot>
          <tbody id='QSnVW'></tbody>
            <bdo id='QSnVW'></bdo><ul id='QSnVW'></ul>
            <legend id='QSnVW'><style id='QSnVW'><dir id='QSnVW'><q id='QSnVW'></q></dir></style></legend>
                <i id='QSnVW'><tr id='QSnVW'><dt id='QSnVW'><q id='QSnVW'><span id='QSnVW'><b id='QSnVW'><form id='QSnVW'><ins id='QSnVW'></ins><ul id='QSnVW'></ul><sub id='QSnVW'></sub></form><legend id='QSnVW'></legend><bdo id='QSnVW'><pre id='QSnVW'><center id='QSnVW'></center></pre></bdo></b><th id='QSnVW'></th></span></q></dt></tr></i><div id='QSnVW'><tfoot id='QSnVW'></tfoot><dl id='QSnVW'><fieldset id='QSnVW'></fieldset></dl></div>

              1. <small id='QSnVW'></small><noframes id='QSnVW'>

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

                  问题描述

                  我想获取一个 URL 并确定它具有什么状态代码,尤其是如果它具有 HTTP 301 或 HTTP 302 状态代码:

                  I want to fetch an URL and determine what status code it has, especially if it has an HTTP 301 or HTTP 302 status code:

                  fetch('https://httpstat.us/301', {
                      redirect: 'true'})
                      .then(response => {
                         console.log(response);
                         const headers = response.headers.entries();
                         for (h of headers) {
                          console.log(h);
                          }
                      }).catch(err => {
                          console.log("in error case")
                          console.log(err);
                      })
                  

                  这确实给了我很多信息,尤其是它通过以下方式告诉我:

                  This does give me a lot of information, especially it tells me via:

                  response.redirected 
                  

                  这是真的.但这只是告诉我它重定向了,而不是状态码是什么.

                  Which is true. But this only tells me that it redirected, not what the status code was.

                  有没有办法使用 Fetch API 来确定请求的真实状态码?

                  Is there a way to use Fetch API to determine the real status code of the request?

                  推荐答案

                  至于为什么代码不能随便检查 response.status:在重定向的情况下,这将是最终返回结果的任何响应的状态代码任何重定向.

                  As far as why the code can’t just check response.status: In the case of redirects, that will be the status code of whatever response ultimately comes back as the result of any redirects.

                  因此,如果重定向最终将返回 200 的内容带到某个地方,那么 response.status 将只是 200.没有中间状态代码暴露给在浏览器中运行的前端 JavaScript — 如 此处发布的先前(非常好的)答案已经指出.

                  So if redirects end up taking things somewhere that returns a 200, then response.status will just be 200. No intermediate status codes are exposed to frontend JavaScript running in a browser—as the previous (very good) answer posted here already points out.

                  这确实给了我很多信息,尤其是它通过以下方式告诉我:

                  This does give me a lot of information, especially it tells me via:

                  response.redirected
                  

                  值得注意的是:仅检测是否存在重定向,实际上您甚至不需要检查.这是最简单的检查方法,但您也可以检查 response.url,如果这与您提供给 fetch(...) 调用的请求 URL 不同,您就知道它被重定向了.

                  It’s worth noting: to just detect if there’s been a redirect, you actually don’t even need to check that. That’s the easiest way to check, but you could also just check response.url, and if that’s different from the request URL you gave to the fetch(…) call, you know it was redirected.

                  这曾经是 response.redirected 首次介绍.

                  That used to be the typical way to check before response.redirected was first introduced.

                  另外值得注意的是:问题代码中的redirect: 'true' 无效;它会导致代码过早地失败.请参阅 https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect:

                  Also worth noting: redirect: 'true' in the code in the question isn’t valid; it’ll cause the code to fail prematurely. See https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect:

                  Value RequestRedirect 枚举值,可以是以下字符串之一:

                  Value A RequestRedirect enum value, which can be one the following strings:

                  • 关注
                  • 错误
                  • 手册

                  所以问题中的代码似乎是 redirect: 'follow'.但这实际上是 redirect 的默认设置,因此您无需显式指定它,而可以省略它.

                  So what the code in the question seems to intend is redirect: 'follow'. But that’s actually the default for redirect, so you don’t need to explicitly specify it and can instead just omit it.

                  至于其他 redirect 值:如 在 Stack Overflow 上对另一个问题的回答,你几乎肯定不想指定 manual,除了一些 Service Worker 案例.而 error 表示将任何重定向都视为网络错误.

                  As far as the other redirect values: As explained in an answer to another question here at Stack Overflow, you almost certainly never want to specify manual except for some Service Worker cases. And error means to treat any redirect as a network error.

                  在这种情况下,对于示例中的代码,redirect: 'true' 会导致 catch 被命中,您将无法访问任何详细信息响应,包括它是否被重定向.

                  In that case, for the code in the example, redirect: 'true' would cause the catch to get hit and you’d have no access to any details of the response, including whether it got redirected.

                  所以回到你原来的问题:

                  So to loop back to your original question:

                  我想获取一个 URL 并确定它有什么状态代码,尤其是如果它有一个 HTTP 301 或 HTTP 302 状态代码

                  I want to fetch an URL and determine what status code it has, especially if it has an HTTP 301 or HTTP 302 status code

                  ...答案是,对于重定向请求,实际上无法从浏览器中运行的前端 JavaScript 代码检测任何重定向的确切状态代码.

                  …the answer is that for redirected requests there really is no way from frontend JavaScript code running in a browser to detect the exact status codes of any redirect.

                  因此,如果您需要知道对给定 URL 的请求可能发生的任何重定向的确切状态代码,您必须从后端代码发出请求,并在那里处理响应.

                  So if you need to know the exact status code for any redirects that may happen for a request to a given URL, you must make the request from backend code, and handle the response there.

                  这篇关于如何确定 Fetch 请求的状态码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何从 fetch API 返回 json 响应 下一篇:每 2 秒获取一次调用,但不希望请求堆积

                  相关文章

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

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

                  1. <legend id='uklAE'><style id='uklAE'><dir id='uklAE'><q id='uklAE'></q></dir></style></legend>