What HTTP error code do I need to return to a jQuery ajax that would work with setting a response method? Using jQuery 1.7.
For example, I have the following code:
var request = $.ajax({
type: "POST",
url: "/report/template/save",
data: formData,
success: function() {
},
statusCode: {
400: function() {
$('#template_form .ajax-msg').show();
$('#template_form .ajax-msg').html('<strong>Sorry, save failed.');
}
}
});
request.fail(function(data) {
$('#template_form .ajax-msg').show();
$('#template_form .ajax-msg').html('<strong>Sorry, save failed.');
});
I can indeed verify a 400 is being returned.
If the form doesn’t validate server-side, I’ve tried returning both a status 500 and 400 with no luck. Instead what happens is that the code is halted and the .ajax-msg doesn’t display anything. What is $.ajax expecting? Thanks!
There were multiple conflicts present and thanks to the comments I was able to sort through it.
First, there were too many success handlers (and in my opinion the naming is a little ambiguous). I removed
statusCodeand replaced withrequest.failandrequest.alwayswhich allowed me to do actions with each response anyways.Second, the code wasn’t actually halting but I mistakenly screwed up on a selector and
$('#template_form .ajax-msg')was changed to'$('.modal .ajax-msg').I hope that helps future problems!