问题
The Response interface of the Fetch API has a read-only flag, redirected
, which indicates whether or not the response was the result of a request that was redirected.
Does the axios library have a similar capability? The best I could find is maxRedirects
which sets the maximum number of redirects to follow. However, I am simply looking to determine if it happened or not (so I can handle redirects specially), not prevent it from happening.
回答1:
If axios exposes the equivalent of the fetch API’s response.url
, you can compare the request URL from your code with the response URL; if those aren’t equal, you know the request was redirected.
As far as the status code being 200, that’s what’s required by the spec; no intermediate status codes are exposed to frontend JavaScript running in a browser — regardless of which API you use. See also the answer at http://stackoverflow.com/a/46031800/441757
回答2:
You can get the redirect count:
const response = await axios.get('http://someurlthatredirects.com');
console.log(response.request._redirectable._redirectCount);
来源:https://stackoverflow.com/questions/55926127/does-axios-have-the-ability-to-detect-redirects