JQuery plugin for connecting an AJAX form - MUST provide a callback

What is the best way to connect AJAX functionality to an existing form using jQuery and allow error handling callbacks.

jQuery.ajax(...) The built-in function has the following (useful) callback functions:

beforeSend

full

dataFilter

Mistake

success

I thought I found my answer with jQuery.Form , but for some crazy reason, they are not giving you access to the error callback !! Beyond this omission, this seems quite useful.

I am really confused as a whole by the number of articles that are recommended to be used jQuery.getbefore they recommend jQuery.ajax. Just for an example: get()doesn't have an error callback, but ajax()does.

-, ajax(), - .

- - -, ASP.NET-MVC, , ?

+3
6

, ( ). , .

(function() {
$.fn.ajaxify = function(options) {
    $(this).submit(function(e) {
        var form = $(this);
        $.ajax({
            type : form.attr('method'),
            url : form.attr('action'),
            data : form.serialize(),
            error : options.error,
            success : options.success,
            dataType : "script"
        });

        return false;
    });
}
})();

:

$('form').ajaxify(
    {success: success_method_name, 
    error: error_method_name});
+6

:

http://www.malsup.com/jquery/form/#api

" , Options jQuery $.ajax. , $.ajax, Options, ajaxForm ajaxSubmit."

, ...

+3

, .

:

$("form#new_asset").ajaxForm({
  error: function(a, b, c) {
    alert('error')
  }
});
+1

, ' , , .

0

JSon? JSon.

-2

Source: https://habr.com/ru/post/1704555/


All Articles