-3

Possible Duplicate:
Can't get correct return value from an jQuery Ajax call

This is an extension of my previous question:

Can I check the return code from a function called with Ajax like this below:

rc =  submitHandler($link, $modal);

function submitHandler($link, $modal) {

           $.ajax({
                url: oSubmit.href,
                dataType: 'json',
                type: 'POST',
                data: $form.serializeArray()
            })
            .done(function (json, textStatus, XMLHttpRequest) {
                json = json || {};
                if (json.success) {
                    submitSuccessModal(oSubmit, json);
                    return true;  <---------------------------------------
                } else {
                    submitFailModal(oSubmit, json);
                    return false;  <--------------------------------------
                }
                return false;
            })

}
Community
  • 1
  • 1
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • 5
    This is a very common question. Please search for "jquery ajax return" and vote to close for one of the duplicates. –  Aug 21 '12 at 16:49
  • 2
    e.g .http://stackoverflow.com/questions/3537434/cant-get-correct-return-value-from-an-jquery-ajax-call , http://stackoverflow.com/questions/4200641/how-to-return-a-value-from-a-function-that-calls-getjson?lq=1 , http://stackoverflow.com/questions/1461895/how-can-i-return-an-ajax-retrieved-value-to-the-parent-function-of-the-current-f?rq=1 –  Aug 21 '12 at 16:50
  • Thanks. I did not realize it was a common question. I did try to delete the duplicate but it said it was not possible as there was already an answer. – Samantha J T Star Aug 21 '12 at 16:59

1 Answers1

0

You can add a function block for each status code respectively

$.ajax({
  statusCode: {
    404: function() {
      alert("page not found");
    },
    500: function() {
      alert("internal server error")
    },
    ...
  }
});
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108