Brian Brian - 1 year ago 110
Javascript Question

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

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


if(d.success == true){
if(d.message != null){
.error(function() {
//need to check for 401 status here to do something

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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download