Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get HTTP error response code in Jquery POST

I have a wrapper function used to send requests to a server, I need to do something specific if the server returns a code of 401 and I'm not sure how to get this data

 $.mobile.showPageLoadingMsg();    
$.post(apiUrl+method,p,function(d) {

    $.mobile.hidePageLoadingMsg();  
    console.log(d.success);
    console.log(d.message);

    if(d.success == true){
        window[callback](d.data);
    }
    else{
        if(d.message != null){
            alert(d.message);
        }  
    }
},'json')
.error(function() {
    //need to check for 401 status here to do something
    $.mobile.hidePageLoadingMsg();  
    alert("error");
});

If my server side code throws a 401 exception the jquery .error function picks this up just because its not 200 OK but I need to check if its a 401 code.

like image 698
Brian Avatar asked Dec 14 '11 23:12

Brian


1 Answers

In the error callback, check xhr.status where xhr is the first argument to the function.

like image 146
alex Avatar answered Nov 08 '22 23:11

alex