views:

1282

answers:

6

Whats the best way to hookup AJAX functionality to an existing Form using jQuery and allow for an error handling callback.

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

beforeSend

complete

dataFilter

error

success

I thought I'd found my answer with the jQuery.Form plugin, but for some crazy reason they don't give you access to an error callback!! Other than this omission it seems quite useful.

I'm really dismayed in general by the amount of articles out there that recommend using jQuery.get before they would recommend jQuery.ajax. Just to recap: get() does not have an error callback, but ajax() does.

In my opinion you should always use ajax() because you always should have some kind of error handling logic.

Are there any other nice plugins - especially something that plays well with ASP.NET-MVC that I should use that make hooking everything up easier?

A: 

Why not to use JSon? You can collect data from form and send JSon request.

Mr.ElectroNick
it will be returning JSon - i'm jsut talking about how to actually hook up the event. see @efalcao's response for the kind of code I was looking for. i want to write this code only once - either myself or in a 3rd party plugin
Simon_Weaver
+4  A: 

This is a very simple snippet that I copied from a project of mine and just hacked it a bit (barely tested). Let me know if this works.

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

using it:

$('form').ajaxify(
    {success: success_method_name, 
    error: error_method_name});
efalcao
getting sleepy but i'll test it out and report back. but yes this is the kind of thing i'm looking for, but probably (success, error) rather than just (error)
Simon_Weaver
I haven't looked but there should be a success callback too
Scott Evernden
ajaxForm certainly has a success callback as well as a beforeSerialize one, which I know i use
Scott Evernden
Yeah that was a late night paste. I'm going to actually test it and have it do success, error. =)
efalcao
@efalcao - probably dont want datatype as 'script'
Simon_Weaver
A: 

Have you tried setting up a handler via $.ajaxError() ?

Scott Evernden
generally i would want the error specific to the component in question. for instance i might have a 'submit question' control which could be used on multiple pages. if there was an error i'd want it to say 'Sorry your question couldn't be submitted at this time'. but i agree ajaxError cant hurt too
Simon_Weaver
+1  A: 

According to this page:

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

"note that the Options Object can also be used to pass values to jQuery's $.ajax method. If you are familiar with the options supported by $.ajax you may use them in the Options Object passed to ajaxForm and ajaxSubmit."

This includes the 'error' handler param, however i can't get it to work...

Rich

A: 

This might not be a problem anymore, cause I just hooked up a error callback just fine.

e.g:

$("form#new_asset").ajaxForm({
  error: function(a, b, c) {
    alert('error')
  }
});
Chris O'Sullivan
A: 

Just tested and Chris O'Sullivan is indeed correct, though at the moment I think that it is undocumented.

timc3