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

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

问题描述

Fetch API的响应界面具有<一个href ="https://developer.mozilla.org/zh-CN/docs/Web/API/Response/redirected" rel ="noreferrer">只读标志,redirected,它指示是否还是响应不是重定向请求的结果.

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.

推荐答案

如果axios公开了与获取API的 response 相同的功能.url,您可以将代码中的请求网址与响应网址;如果这些不相等,则说明请求已重定向.

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.

状态码为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天全站免登陆