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.

2
  • When a http request return as 302, the browser do the redirect internally. I think that you cannot catch this. See: stackoverflow.com/questions/18737674/… Commented Jul 6, 2017 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. Commented Jul 6, 2017 at 13:26

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ok that is the right answer but I'm looking for solution, how can i get through this problem in the easiest way.
there is no solution. Thats how our browsers are designed. You can change this behaviour for your browser.
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.