最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS $http: how to avoid redirect following - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Dec 26, 2014 at 9:51 MarcoMarco 1,6523 gold badges16 silver badges29 bronze badges 1
  • Not true. As I said the result of $http.post call is the result of the final destination. – Marco Commented Dec 26, 2014 at 10:10
Add a ment  | 

1 Answer 1

Reset to default 8

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

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

The W3C spec for XMLHttpRequest says

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.

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

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.

发布评论

评论列表(0)

  1. 暂无评论