JQuery Form Plugin - Refuse Callback Function?

For the jquery form plugin http://jquery.malsup.com/form/#options-object , I can see the success callback, but how do I run the code if there is a glitch like a network problem?

+2


a source to share


3 answers


From the site:

Besides the options listed below, you can also pass any of the standard $ .ajax options to ajaxForm and ajaxSubmit

Presumably this means that you can simply add the "error" property with the function you want to execute on error.



Take a look at the plugin code that looks like this:

$.fn.ajaxForm = function(options) {
    ...
    $(this).ajaxSubmit(options);
    ...
}

...

$.fn.ajaxSubmit = function(options) {
    ...
    $.ajax(options);
    ...
}

      

+7


a source


Callback handler

'error' must be enabled:



"Note: In addition to the parameters listed below, you can also pass any standard $ .ajax options to ajaxForm and ajaxSubmit." ( http://jquery.malsup.com/form/#options-object )

+2


a source


I have never used this plugin before, try with:

error: function() { }

      

as in the usual $ .ajax from jQuery. if that doesn't work, you can always do a workaround on your server:

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

      

+2


a source







All Articles