如何确定 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.
相关文章