Is it possible to handle 302 status code when using $http.get and $http.post?

18,880 views
Skip to first unread message

ssaniel

unread,
Apr 8, 2015, 2:15:27 AM4/8/15
to ang...@googlegroups.com
Hi, 

I've posted several times in the stackoveflow about this question but no one seems to understand my question or maybe they don't have the time to read my entire post because they kept on giving me "wrong" answers.

Im using $http post and get in my project. Now, I'm having an issue with redirection. I get a 302 status code when the session expires and when I checked the response header I see that Location contains the url for my login page which is correct. Now the problem is that the page isn;t redirected to login. Is it because I used $http? It stays in the home page. If its true that the browser should handle redirection status codes, then why is it not redirecting to where it is supposed to be redirected?  Anyway, this is when I started resaerching on how to get the redirection status code and i will create a code that will redirect the page to login if the code sees that it receives a redirect code.

Some of the response from stackoverflow was to add this to my code. But some argued that we cannot get 302 or any other redirect code.  

app.factory('myHttpResponseInterceptor', ['$q', '$location', function($q, $location) {
    return function(promise) {
        var success = function(response) {
            if (response.status === 302) {
                alert("success  " + response.status);
                $location.path('/login.html');
                return response;
            } else {
                alert("success  " + response.status);
                return response;
            }
        };

        var error = function(response) {
            if (response.status == 401) {
                if (response.status === 302) {
                    alert("error " + response.status);
                    $location.path('/public/login.html');
                    return $q.reject(response);
                } else {
                    alert("error  " + response.status);
                    return $q.reject(response);
                }

            }

            return $q.reject(response);
        };

        return promise.then(success, error);
    };
}]);

//Http Intercpetor to check auth failures for xhr requests
app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('myHttpResponseInterceptor');
}]);

Is it possible to handle 302 status code when using $http.get and $http.post? how?

Gaurav Ashara

unread,
Apr 8, 2015, 3:06:45 AM4/8/15
to ang...@googlegroups.com
Hello,

I think you can try following code to redirect.


$window
.location.href ="URL";



Thanks & Regards
Gaurav Ashara

ssaniel

unread,
Apr 8, 2015, 3:17:24 AM4/8/15
to ang...@googlegroups.com
Hi Gaurav,

Yes I already did that. But I need to know first what the status code is. That's what I want to know,

Thanks,
ss

Sander Elias

unread,
Apr 8, 2015, 4:35:28 AM4/8/15
to ang...@googlegroups.com

Hi Gaurav

if (response.status == 401) { 
   if (response.status === 302) {

Will never reach the 302 status. Well, not until 401 and 302 will be equal to JS ;)

I’m not sure if that’s your problem, but that’s what my eye did catch on a quick glimpse

Regards
Sander

Gaurav Ashara

unread,
Apr 8, 2015, 5:02:16 AM4/8/15
to ang...@googlegroups.com
Hello Sander Elias,

You are right.I just miss out it. I think it just problem of redirect.

Thanks for your correct guidance, You always did good job.

ssaniel

unread,
Apr 12, 2015, 9:44:32 PM4/12/15
to ang...@googlegroups.com
Hi Sander,

So my concern here is that, I don't get redirected to the url specified in the (header) location. That's why I want to handle redirect on my own. But apparently, most of the people that's using angular are having the hard time answering my question, which makes me think that this is a rare question, meaning, people don't have any problem with redirection the same as I have. I would like also to state the fact that since developer of angularjs didn't bother on creating a callback for redirection (2XX = success and 4XX and 5XX = error) it makes me think that it is really the browsers job to do that. 

I was thinking that the problem has something to do with the web server that I'm using. When I asked the support of the web server, this was his reply ...

"If you are using Angular and doing Ajax requests, you need to check the status and when you get 302, you need to redirect the client by setting window location. Normal client side stuff. "

To confirm his claim, my question is, was $http created using Ajax? And do I really need to handle redirect status when Im using $http?

Thank you,
ssaniel 

Sander Elias

unread,
Apr 12, 2015, 11:56:26 PM4/12/15
to ang...@googlegroups.com
Hi Ssaniel,


I agree that one would expect that browsers would handle this on their own. The do if you navigate to such an URL, so why not on 302 one would say?. I can imagine situation where you don't want that if you are creating your own app, so that might be the whole reason.
But 302's are not that common. you don't see them often, and I think that is the reason those are overlooked in almost any framework.

So, indeed, you have to handle it on your own. Luckily for you, you chose angular. You can create an interceptor that encapsulates the handling of 302's.

Hope this helps you a bit.
Regards
Sander

ssaniel

unread,
Apr 13, 2015, 3:14:55 AM4/13/15
to ang...@googlegroups.com
Hi Sander,

Yes! Thank you for the clarfication. One last question, what interceptor will catch 302? I've already tried response and response error. Both couldn't catch 3XX?

Best regards,
Ssaniel

Sander Elias

unread,
Apr 13, 2015, 3:50:05 AM4/13/15
to ang...@googlegroups.com
Hi Ssaniel,

I just checked for you, but I was wrong this morning. (coffee did set yet :) ) Most browsers do handle the 302 on their own. However, it happens that in most case you get an 302 redirect to login, witch then gives a 40x (2 mostly) that the user is not authenticated.
So, you can tell your server guy, that they are sending back the wrong response. if auth is needed, they should send back an 402, not an 302. (or you can work around it, often an easier solution.)

Regards
Sander

ssaniel

unread,
Apr 13, 2015, 4:21:55 AM4/13/15
to ang...@googlegroups.com
Hi Sander,

Okay thank you. Got it. But assuming I want to make a work around. How will I catch 302? what Interceptor should I use? because Ive already tried using request, response, responseError and requestError Interceptors. All of it couldn't catch the 302 status :| 

Best regards,
Ssaniel

Sander Elias

unread,
Apr 13, 2015, 4:54:14 AM4/13/15
to ang...@googlegroups.com
That's correct, those get caught by the browser, and don't reach your app normally. The usual way to catch those is in the response handler. there you can inspect config/headers/payload. sometimes it helps just to dump those to the console and go from there!

Regards
Sander
Reply all
Reply to author
Forward
0 new messages