by Admin
24. July 2009 08:14
In my previous posts How to serialize AJAX requests and others, I have been using simple jQuery method .getJSON to get response from server. You can see that this method takes only 3 parameters and there is not much you have to set or do to set up your AJAX calls and it works for 90% of the cases. But some time you run into situation where you need little bit more control on how the request is set up or want to control the request a little bit. jQuery does provide you excess to underlying structures to set some parameters for the calls or you can use the low level .ajax. A lot of time you really are not looking to use .ajax function but you want to set some default behavior for all the AJAX requests that are being send using .getJSON. To do that you can use .ajaxSetup method. This function has one parameter where you can set up all the AJAX call parameters in a structure. For example in my application, I want to make sure that all AJAX calls time out in 10 second and I also want that all errors should be taken care of in one function. So I called .ajaxSetup method at load time to set up the behavior.
$(function() {
$.ajaxSetup({
timeout : 10000,
error: errorDisplay,
sync: true
});
elClockDisplay = $('#clockDisplay');
elLatencyDisplay = $('#latencyDisplay');
elUpdatesPanel = $('#siteUpdatesPanel');
getServerTime();
});
There are lot of other properties that you can set in this structure. I will recommend reading jQuery for latest information about these properties. I will list some of the common ones that you may run into.
- type
- beforeSend
- timeout
- processData
- dataTyoe