Leveling Up AJAX
Not a beginner-level article
We have three ways to "level up" AJAX in jQuery: creating prefilters, adding new converters, and transports.
Prefilters
A prefilter is a function that will be called before the ajaxStart event, where you can modify both the jqXHR object and any associated settings:
// register an AJAX prefilter
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
// our manipulations on settings and jqXHR
});What's all this for? Here's a simple task — don't wait for an "old" AJAX response if we're requesting the same URL again:
// collection of current requests
var currentRequests = {};
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
// our custom setting
if ( options.abortOnRetry ) {
if ( currentRequests[ options.url ] ) {
// abort the old request
currentRequests[ options.url ].abort();
}
currentRequests[ options.url ] = jqXHR;
}
});
// call using the filter
$.ajax({
/* ... */
abortOnRetry: true
})You can also modify the call options — here's an example that redirects the request to a pre-prepared proxy page on our server based on the crossDomain flag:
Prefilters can be attached to a specific dataType (i.e., different filters will fire depending on the expected data type from the server):
And lastly: to switch the dataType to some other type, we just need to return the desired value:
Converters
A converter is a callback function that's called when the received data type doesn't match the expected one (i.e., dataType was specified incorrectly).
All converters are stored in the global ajaxSettings:
To extend the set of converters, you'll need the $.ajaxSetup() method:
Converters should be used when you need to introduce custom dataType formats or to convert data into the desired format. Specify the required dataType when calling $.ajax():
Converters can also be specified directly in the $.ajax() call, so as not to clutter up the global settings:
A bit of explanation: we request "XML", which we convert to text, which will then be passed to our converter from "
text" to "mydatatype".
Transport
A transport is an object that provides two methods — send() and abort() — which will be used inside the $.ajax() method. To register your own transport method, use $.ajaxTransport(). It'll look something like this:
Let me briefly explain the parameters we'll be working with:
options
request settings,
i.e., what we specify when calling $.ajax()
originalOptions
"clean" settings,
without even the default changes applied
jqXHR
the jQuery XMLHttpRequest object
headers
request headers as key-value pairs
completeCallback
callback function to be used for notifying about request completion
The completeCallback() function has the following signature:
where:
status
HTTP status of the response
statusText
text interpretation of the response status
responses
optional — an object containing server responses in all formats supported by the transport;
for example, the native XMLHttpRequest would look like { xml: XMLData, text: textData } when requesting an XML document
headers
optional — a string containing the server response headers, if the transport can obtain them;
for example, the XMLHttpRequest.getAllResponseHeaders() method can handle this
Just like prefilters, transports can be bound to a specific requested data type:
And now for a brain-buster — let's add an "image" transport to our page:
Now we can use this transport:
I'd like to remind you once more that this is "advanced level" material, and this section is arguably out of place in a "beginner" textbook.
Following the official documentation:
Last updated