JQuery Form Plugin - Callback function for failure?
For the jquery form plugin http://jquery.malsup.com/form/#options-object, I see a success callback, but how do I run code if there is a 开发者_开发知识库failure like a network issue?
From the site:
Aside from the options listed below, you can also pass any of the standard $.ajax options to ajaxForm and ajaxSubmit
Presumably that means you could just add an "error" property with the function you'd like to execute upon error.
Looking at the plugin code that seems to be the case:
$.fn.ajaxForm = function(options) {
...
$(this).ajaxSubmit(options);
...
}
...
$.fn.ajaxSubmit = function(options) {
...
$.ajax(options);
...
}
'error' callback handler should be enabled:
"Note: Aside from the options listed below, you can also pass any of the standard $.ajax options to ajaxForm and ajaxSubmit." (http://jquery.malsup.com/form/#options-object)
I've never used that plugin before, try with:
error: function() { }
like in normal $.ajax from jQuery. if that does not work you can always make a workaround on your serverside code:
JsonResponse ActionMethod(params) {
try {
// logic goes here
}
catch (e) {
return json(new { error = e.message });
}
}
in your javascript
$.ajaxForm({
success: function() {
if (data.error) {
alert(error);
}
else {
//logic goes here
}
}
});
精彩评论