0

I'm trying to write Angular application with $routeProvider. I have .NET on backend that is serving htmls for my app (prepared with permissions and roles). On some action backend calls for RedirectToAction that means 302 HTTP to another url and that means my templateUrl inside router gets another html that it should with that route. I must catch this 302 HTTP somehow, and change route to another (or just use another controller).

Propably the best option is to rewrite backend and use API requests that would check if I need this redirection, but what to do when I can't change it?

I tried to use interceptor in $httpProvider but that response has HTTP 200 and just data of redirected site.

JJaniszewski
  • 25
  • 1
  • 5
  • When a http request return as 302, the browser do the redirect internally. I think that you cannot catch this. See: https://stackoverflow.com/questions/18737674/handle-http-302-response-from-proxy-in-angularjs – Pedro Ramon Jul 06 '17 at 13:16
  • I read it, but this "hack" doesn't work in my case. I can put in html which controller/path should be enabled, but then i should check in every single controller if it is the one i'm looking for which is impossible for me in big app. – JJaniszewski Jul 06 '17 at 13:26

2 Answers2

1

You can not handle 302 response from a server because browsers do this before the Angular is notified. In a way, Angular response interceptor will never get a hand on this response.

It is properly explained :here Handle HTTP 302 response from proxy in angularjs or

ASLAM TP
  • 170
  • 11
  • Ok that is the right answer but I'm looking for solution, how can i get through this problem in the easiest way. – JJaniszewski Jul 06 '17 at 13:28
  • there is no solution. Thats how our browsers are designed. You can change this behaviour for your browser. – ASLAM TP Jul 06 '17 at 13:32
0

I think it should not be the best way, but think about returning an http 200 and an object with a fild that tells the angular to redirect.

Some like:

{ location: "/my-new-location" }

And then, do the redirection in the application instead of doing in the backend!

Pedro Ramon
  • 124
  • 1
  • 4