AngularJS $ HTTP:如何避免重定向以下 [英] AngularJS $http: how to avoid redirect following

查看:629
本文介绍了AngularJS $ HTTP:如何避免重定向以下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个AngularJS应用程序,我做出$ http.post呼叫服务器的URL。此URL的成功响应是一个30倍的重定向,我的问题是,$ http.post严格遵循该重定向并返回的成功回调的最后调用的结果。
我想这拦截重定向和手工重定向用户(改变浏览器的URL)的最后一页。难道不可能性?

In an AngularJS application I make an $http.post call to a server url. The success response of this url is a 30x Redirect, my problem is that $http.post strictly follows this redirect and returns in the success callback the result of the final invocation. I would like to intercept this redirection and manually redirect the user (changing the browser url) to the final page. Is it possibile?

推荐答案

Accoring到文档,在 $ HTTP 服务是核心角服务,通过浏览器的XMLHtt prequest对象或通过JSONP与远程的HTTP服务器促进沟通。

Accoring to the docs, the $http service is a core Angular service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP.

知道了,知道了XMLHtt prequest规格,它可能无法截获重定向。

Knowing that, and knowing the specifications for XMLHttpRequest, it's probably not possible to intercept a redirect.

的<一个href=\"https://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#infrastructure-for-the-send%28%29-method\"相对=nofollow> W3C规范为XMLHtt prequest 说:

如果响应有HTTP状态code 301,302,303,307,308或

如果重定向违反无限循环precautions这是一个网络
  错误。

If the redirect violates infinite loop precautions this is a network error.

否则,执行这些步骤:

设置请求URL通过Location头所传达的URL。

Set the request URL to the URL conveyed by the Location header.

如果源产地和请求URL的来源是同根同源
  透明跟随重定向,同时观察同源
  请求事件的规则。

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用户代理有关preservation要求地方
  请求方法和重定向期间请求实体正文,和
  还需要最终用户通知某些种类的自动
  重定向。

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.

因此​​重定向发生之前很久角度可以做任何事情,它不能停止。

So the redirect happens long before Angular can do anything about it, and it can't be stopped.

另一种选择是做 $ HTTP 打电话给自己的服务器,然后使用类似卷曲服务器端(这取决于语言的使用服务器端),它可以设置为不进行重定向,获取资源。

Another option would be to do the $http call to your own server, and then use something like cURL serverside (depending on what language is used serverside) which can be set to not follow redirects, to get the resource.

这篇关于AngularJS $ HTTP:如何避免重定向以下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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