JJaniszewski
JJaniszewski

Reputation: 35

AngularJS Router and .NET backend - getting redirect on templateUrl

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.

Upvotes: 0

Views: 251

Answers (2)

Pedro Ramon
Pedro Ramon

Reputation: 126

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!

Upvotes: 0

ASLAM TP
ASLAM TP

Reputation: 170

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

Upvotes: 1

Related Questions