angular: 302 server status code redirection is not workin
I’m submitting a … (check one with “x”)
[x ] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior When we make a get request, server send us back a 302 to the same SPA. Angular2 is not handling the redirection, and we have a 404 error in browser It is working fine if I copy paste myself the link in browser.
Expected behavior It should redirect properly to the page and route to the component based on the url.
Minimal reproduction of the problem with instructions 1/ Make a get request 2/ Server send back a 302 3/ Browser console log a 404 error. (see below provided code) What is the motivation / use case for changing the behavior? Angular2 should be able to handle this behavior because 302 is used in Auth2 standard.
Please tell us about your environment:
Angular
- Angular version: 2.4.0
-
Browser: [all ]
-
Language: [ TypeScript 2.1]
–Esxtra code:
Node server:
app.get('/app', function(req, res) {
console.log(req.query.redirecturi);
res.writeHead(302, {
'Location': req.query.redirecturi,
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
//add other headers here...
});
res.end();
});
Angular2 request
this._http.get("http://localhost:3000/app")
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 35 (3 by maintainers)
I’m having the same problem. I am calling a api that sends a 302, which I have no control over.
What’s interesting to me, is if I hit the endpoint directly in my browser, my browser does the right thing and will redirect to the Location provided by the 302 response.
But when Angular handles it, it just fails. It does not handle the redirect at all. People are saying this is a browser issue and not an Angular issue, but if that were true, I’d expect the browser to fail too when I hit the endpoint directly through my browser (not through the angular frontend). What’s more, is people here are suggesting it worked on Ng 1… Perhaps this should have a second look.
As much as has been said about this not being an “Angular” problem, it is. We’ve had code in production for months that handled this exact scenario. However, after upgrading to the new HttpClient this seems to be a breaking change. I even consulted the source for the HttpClient and there is nothing in it that would suggest a 302 response would be swallowed.
To be clear, I don’t expect this to handle the redirect - my existing code does that fine. My issue is that I no longer see any response when it has a status code of 302 (except for my in my browser’s network tab.) Previously my error handling watched for the 302 and redirected manually. If this isn’t a behavior that the Angular team plans to implement - fine. But this definitely needs to be included as a breaking change.
After an unpleasant exchange of messages in Stackoverflow, I understood the problem. when Angular makes a request that results in a 302 response, it is an ajax request. Then, Angular http will do the next call to the new url. What we want is the browser acting, redirecting it. But it will not ocurr. Is not a browser request that is redirecting. Is an ajax request.
We are seeing this same behavior when posting to an API that responds with a 302. It looks like the Angular HTTP client is trying to re-post using the redirect URL, which doesn’t result in an actual redirect in our case.
2 or 3 solutions :
1/ We had to do it because of oAuth2 standard. So had a temporary workaround instead of doing a get we do : window.replace(). 2/ Angular 4.2 (or 3) implemented an interceptor :https://angular.io/guide/http#intercepting-all-requests-or-responses . However I don’t like this solutions. 3/ We will change next month the server side implementation of oAuth2 and intead of doing a ‘get’, we will go a ‘post’.
Hope it helps.
i have the same problem just change in the server side the status of Response from 302 to another statut like 200
I have set proxy too.
Please give me some solutions.
I’ve a little bit problem like this. When server returns a 302 code, i WANT to be redirected. But, Angular fails - my browser doesn’t redirect.