I have a form that is being serialized by JQuery and posted via .ajax() to a url.
The problem is that the 'success:' function is always called, regardless of whether the server returns an error code or not.
In fact, success fires even before the server has responded (I have put a breakpoint on the server method that services the request - success fires even before this method is completed). If the server returns an error code (e.g. Status code 500) JQuery calls开发者_StackOverflow社区 BOTH success and error events!
Any ideas what's going on? Here's my jquery code:
$("#a-dialog").dialog({
autoOpen: false,
height: 300,
width: 400,
modal: true,
buttons: {
"Submit": function() {
$.ajax({
type: 'POST',
url: theURL,
data: $("#a-dialog-form").serialize(),
success: alert('ok!') // THIS IS ALWAYS CALLED (IMMEDIATELY)
});
},
},
});
UPDATE:
This was a stupid error on my part! Thanks to blue112 for quickly pointing it out :)
That's normal, you must pass it as a callback, eg
$.ajax({
type: 'POST',
url: theURL,
data: $("#a-dialog-form").serialize(),
success: function(){alert('ok!');} // The function will be called upon success.
});
You need to provide success with a pointer to a function, or, as commonly used, by passing the function definition.
What you have to do is have something like this:
"Submit": function() {
$.ajax({
type: 'POST',
url: theURL,
data: $("#a-dialog-form").serialize(),
success: function (data) {
alert('ok!') // THIS IS ALWAYS CALLED (IMMEDIATELY)
}
});
},
},
What's happening in your case is that the alert is called right away so you will see the alert message even if the ajax request didn't get fired at all.
精彩评论