Axios 是否具有检测重定向的能力? [英] Does Axios have the ability to detect redirects?

查看:44
本文介绍了Axios 是否具有检测重定向的能力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fetch API 的响应接口有一个只读标志重定向,表示响应是否是重定向请求的结果.

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.

axios 库是否具有类似的功能?我能找到的最好的是 maxRedirects ,它设置要遵循的最大重定向数.但是,我只是想确定它是否发生(因此我可以专门处理重定向),而不是阻止它发生.

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.

推荐答案

将您的代码中的请求 URL 与 response.request.responseURL 给出的 URL 进行比较;如果它们不相等,您就知道请求已被重定向.

Compare the request URL from your code with the URL given by response.request.responseURL; if those aren’t equal, you know the request was redirected.

就状态码为 200 而言,这是规范所要求的;无论您使用哪种 API,都不会向浏览器中运行的前端 JavaScript 公开中间状态代码.另请参阅 http://stackoverflow.com/a/46031800/441757

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

这篇关于Axios 是否具有检测重定向的能力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆