javascript - AngularJS $http : how to avoid redirect following

标签 javascript angularjs redirect

在 AngularJS 应用程序中,我对服务器 url 进行 $http.post 调用。此 url 的成功响应是 30x 重定向,我的问题是 $http.post 严格遵循此重定向并在成功回调中返回最终调用的结果。 我想拦截此重定向并手动将用户重定向(更改浏览器 url)到最终页面。可能吗?

最佳答案

根据docs$http 服务是一个核心 Angular 服务,它通过浏览器的 XMLHttpRequest 对象或通过 JSONP 促进与远程 HTTP 服务器的通信。

了解这一点,并了解 XMLHttpRequest 的规范,拦截重定向可能是不可能的。

W3C spec for 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/

相关文章:

javascript - 检查 jQuery 中另一个页面的值

javascript - 是否使用 : var app = angular. 模块...或简单地使用 : angular. 模块(

javascript - 如何整合跨站多 Angular 应用?

javascript - 如何在访问时添加iframe当前域?

redirect - 子域永久重定向到 www。在 lighttpd 中?

javascript - jquery 只允许删除一个对象

javascript - 如何在表中激活 Bootstrap 日期选择器?

css - 如果 'user' 已登录,我想每次都调用自动检查。如果不是 'user' 登录,然后重定向回(url)

javascript - 有什么办法可以避免 Canvas 中的跨域图像问题?

javascript - 使用 AngularJS 解析 JSON