Error callbacks in specialized jQuery AJAX functions
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 an开发者_开发百科d 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?
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.
$.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
精彩评论