Reputation: 3199
I want to access the return statusCode as 200/401 etc.How can I do this?
var poster=$.post(url,
{nickname:name, email:mail},function(data) {
alert('Inside poster done');
});
alert(poster.statusCode);
poster.done(function(data){
alert(data);
});
I have tried the answer given in How do I get HTTP error response code in Jquery POST (xhr.status), and it simply returns 0.Thought the actual response is 401 which was shown in the web console.What's the right way of accessing the return status of the POST command?
Upvotes: 0
Views: 1512
Reputation: 123533
If by "returned" you mean "in the response from the server," you can access the value inside a callback, especially with .always()
so it's called whether there's an error or not:
poster.always(function(data){
alert('status: ' + poster.status);
});
Outside of those callbacks, the statement will be evaluated when the request is still pending completion. So, poster.status
will be its default value, 0
.
However, if you mean you want to:
return poster.statusCode;
Then my point about the request still pending again applies. You just can't do this and get the actual value with an asynchronous request.
See How do I return the response from an asynchronous call? for more. It focuses on the responseText
, but status
is set at about the same time.
Upvotes: 1
Reputation: 11
although success and error call back is generated from response code but you can do it this way too. look here http://api.jquery.com/jQuery.ajax/
$.ajax({
statusCode: {
404: function() {
alert("code 404");
}
200:function(){
alert("code 200");
}
}
});
Upvotes: 1