开发者

JQuery Form Plugin - Callback function for failure?

开发者 https://www.devze.com 2022-12-30 01:34 出处:网络
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:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号