tags:

views:

87

answers:

3

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?

+1  A: 

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);
    ...
}
CalebD
Guess I skimmed the page too quickly :). Thanks.
Kyle
A: 

'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)

mamoo
+1  A: 

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
        }
    }
});
GerManson