0

I'm making ajax requests using the following format:

$post('url', {someData:true}, myCallbackFunction, 'json);

The problem is, if on the server side any status code other than 200 is returned, the callback function is never called.

For example, if the user's session has timed out, I want to return 401 (unauthorized) as the status code, alongwith a JSON object as result which has an error message. But, unless the status code is 200, the callback function doesn't get called.

Any ideas how to work around this?

1
  • 1
    Try using .done() and .fail(), or even just .fail() if your success callback is working fine. Commented Aug 7, 2013 at 21:39

1 Answer 1

3
function errorCallback(response){
    alert(response.status + ' ' + response.statusText );
}
$.post('url', {someData:true}, myCallbackFunction, 'json').fail(errorCallback);

See jquery.post for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

this one works, but i want to get the JSON response returned by the server to be passed into the error callback. Any idea how to get the server response passed as an argument into the error callback?
.fail(function(result) { myErrorCallback(result.responseJSON); } worked. responseText should work for normal / non json response.s

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.