views:

52

answers:

2

I am a long-time, generally very happy user of Prototype. I recently switched to jQuery because of the massive community support, basically amounting to a unanimous choice and de facto industry standard. Since then I have not been so happy. Yes, I have read the comparison threads, and I can live without the handful of ported Ruby/Rails convenience functions, like first(), last(), inspect(), collect() etc, and I get re-familiarize myself with the select syntax and even have to agree it can be powerful. But I have to say I am hung up on the lack of error callbacks for $.post(), $.get(), and even the whole form plugin?!

Is the standard in jQuery really to use the barebones ajax method whenever you need an error callback? My ajax form handlers are 20-25 lines long now when they used to be 5-10. Is it really advisable not to have error callbacks in a real web application? Or am I missing something?

A: 

$.post and $.get are shorthand helper methods that wrap the functionality of $.ajax. Look at $.ajax to see error callbacks and all that stuff. http://docs.jquery.com/Ajax/jQuery.ajax#options

Also take a look at the ajaxEvents for more generic error/progress handling. For example, makes it easy to bind to ajaxSend to show a progress indicator somewhere on the page. http://docs.jquery.com/Ajax_Events

Infinity
So error callbacks should work with $.get and $.post (and json etc)?What about the following code from docs: // NOTE: Apparently, only "success" is returned when you make // an Ajax call in this way. Other errors silently fail. // See above note about using $.ajax.
floyd
Only the $.ajax method supports error callbacks, but coming from Prototype I don't think you'll mind using $.ajax for everything, or maybe you could write new functions similar to $.get/post but that support error callbacks, wrapping $.ajax
Infinity
Just saw the newer docs, and the info for post specifically identifies the success callback, with no mention of an error callback. This is at least highly confusing...
floyd
Might be a bit confusing.. but makes sense if you see that $.post takes 4 parameters `url, [data], [callback], [type]`, instead of a `options` object like $.ajax
Infinity
A: 

Using the ajax method really shouldn't take much more than using post/get. You have to use a different format (map of property/values) because the number of arguments is variable, but I don't see that as onerous.

$.post( url, data, success_cb, type )

becomes

$.ajax({
   url: url,
   data: data,
   type: 'post',  // the only extra thing
   dataType: type,
   success: success_cb }
});

and you can add in the error, complete, etc. callbacks if you want.

tvanfosson
I guess then I want to know, who is using AJAX in a web application without giving the user any feedback about errors? As you say yourself, they don't save much (although this isn't the case for the form plugin), and they make, say validations impossible. Even just for the case of a unforeseen server error, do you really want it to just fail silently? From this point of view, it confuses me they would be included in the core lib.
floyd
There are cases where you don't really care about errors, like say, logging a user interaction. Would you really want to tell the user that his click action on link 'foo' didn't get recorded correctly? But for anything else, $.ajax is the way to go
Infinity