0

I'm trying to make a form debugger to my app, and using jquery.form.js to submit the form.

But I want to catch the status code on both success or error, how can I do it?

I tried this, but the sta variable only returns the 'success' or 'error', not the status code I expected, plz help.

$('form').ajaxSubmit({
    success: function(res, sta, xhr, $form) {
        $('#result-code').html(sta);
        $('#result-content').html(res);
    },
    error: function(res, sta, xhr, $form) {
        $('#result-code').html(sta);
        $('#result-content').html(res);
    }
});

as @Rainer Rillke replied: the solution is below, notice that the parameters sequence received of error is differ from the one with success!

$('form').ajaxSubmit({
    success: function(res, sta, xhr, $form) {
        $('#result-code').html(xhr.status);
        $('#result-content').html(res);
    },
    error: function(xhr) {
        $('#result-code').html(xhr.status);
        $('#result-content').html(xhr.responseText);
    }
});
2
  • Assuming you are using jquery.form plugIn and want to capture the HTTP server status, use the xhr.status and xhr.statusText properties of the jqXHR. Commented Jun 4, 2014 at 1:21
  • @Rainer Rillke: I tried, but still cannot got it right, I updated the question, plz help. Commented Jun 4, 2014 at 1:30

2 Answers 2

2

Use the xhr.status property of the jqXHR.

$('form').ajaxSubmit({
    success: function(res, sta, xhr, $form) {
        $('#result-code').text(xhr.status);
        $('#result-content').html(res);
    },
    error: function(xhr) {
        $('#result-code').text(xhr.status);
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

I got undefined on both xhr.status and xhr.statusText.
Updated response. Your parameter order was wrong. Use console.log(arguments) if you are not sure what you get.
I got xhr to be a string of value "METHOD NOT ALLOWED" but not an object.
Oh, I found the point, the argument list is different, I have update the solution to the question, thank you!
0

I'm not sure if it's some kind of custom error that you're trying to catch, but if you're just trying to return the error that is coming from the web page, do a try/catch:

try {
     //do something here
}

catch (e) { //e = exception
     alert(e.toString());
}

2 Comments

The procedure don't even raise an error. I found the answer to the standard jquery function here: stackoverflow.com/questions/12759627/…, but I'm using jquery.form.js plugin for ajax submitting the form. I want the solution in this case.
Names of jQuery plugIns aren't unique. Links are cheap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.