在 AngularJS 应用程序中,我对服务器 url 进行 $http.post 调用。此 url 的成功响应是 30x 重定向,我的问题是 $http.post 严格遵循此重定向并在成功回调中返回最终调用的结果。 我想拦截此重定向并手动将用户重定向(更改浏览器 url)到最终页面。可能吗?
最佳答案
根据docs ,$http
服务是一个核心 Angular 服务,它通过浏览器的 XMLHttpRequest 对象或通过 JSONP 促进与远程 HTTP 服务器的通信。
了解这一点,并了解 XMLHttpRequest 的规范,拦截重定向可能是不可能的。
If the response has an HTTP status code of 301, 302, 303, 307, or 308
If the redirect violates infinite loop precautions this is a network error.
Otherwise, run these steps:
Set the request URL to the URL conveyed by the Location header.
If the source origin and the origin of request URL are same origin transparently follow the redirect while observing the same-origin request event rules.
Otherwise, follow the cross-origin request steps and terminate the steps for this algorithm.
HTTP places requirements on the user agent regarding the preservation of the request method and request entity body during redirects, and also requires end users to be notified of certain kinds of automatic redirections.
因此,重定向发生在 Angular 对其采取任何措施之前很久,并且无法停止。
另一种选择是对您自己的服务器执行 $http
调用,然后使用诸如 cURL 服务器端之类的东西(取决于服务器端使用的语言),它可以设置为不遵循重定向,获取资源。
关于javascript - AngularJS $http : how to avoid redirect following,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27655449/