jquery 1.5 ajax的改进
源文见: http://api.jquery.com/extending-ajax/
相比之前的版本, 重写了ajax模块, 引入更多的扩展点. 三个概念:
PrefiltersA prefilter is a callback function that is called before each request is sent, and prior to any $.ajax() option handling.
Prefilters are registered using $.ajaxPrefilter(), and a typical registration looks like this:
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
// Modify options, control originalOptions, store jqXHR, etc
});
- options are the request options
- originalOptions are the options as provided to the ajax method, unmodified and, thus, without defaults from ajaxSettings
- jqXHR is the jqXHR object of the request
It is also possible to attach a prefilter to requests with a specific dataType. For example, the following applies the given prefilter to JSON and script requests only:
$.ajaxPrefilter( "json script", function( options, originalOptions, jqXHR ) {
// Modify options, control originalOptions, store jqXHR, etc
});
Converters (注册自己的数据转换处理)A converter is a callback function that is called when a response of a certain dataType is received while another dataType is expected.
Converters are stored into ajaxSettings and can be added globally as follows:
$.ajaxSetup({
converters: {
"text mydatatype": function( textValue ) {
if ( valid( textValue ) ) {
// Some parsing logic here
return mydatatypeValue;
} else {
// This will notify a parsererror for current request
throw exceptionObject;
}
}
}
});
Converters are useful to introduce custom dataTypes. They can also be used to transform data into desired formats. Note: all custom dataTypes must be lowercase.
With the example above, it is now possible to request data of type "mydatatype" as follows:
$.ajax( url, {
dataType: "mydatatype"
});
或者也可以直接写在ajax单独的一次处理里
$.ajax( url, {
dataType: "xml text mydatatype",
converters: {
"xml text": function( xmlValue ) {
// Extract relevant text from the xml document
return textValue;
}
}
});
Transports (自定义整个的传输过程, 这是迫不得已才用)A transport is an object that provides two methods, send and abort, that are used internally by $.ajax() to issue requests. A transport is the most advanced way to enhance $.ajax() and should be used only as a last resort when prefilters and converters are insufficient.
Since each request requires its own transport object instance, tranports cannot be registered directly. Therefore, you should provide a function that returns a transport instead.
Transports factories are registered using $.ajaxTransport(). A typical registration looks like this:
$.ajaxTransport( function( options, originalOptions, jqXHR ) {
if( /* transportCanHandleRequest */ ) {
return {
send: function( headers, completeCallback ) {
/* send code */
},
abort: function() {
/* abort code */
}
};
}
});
- options are the request options
- originalOptions are the options as provided to the ajax method, unmodified and, thus, without defaults from ajaxSettings
- jqXHR is the jqXHR object of the request
- headers is a map of request headers (key/value) that the transport can transmit if it supports it
- completeCallback is the callback used to notify ajax of the completion of the request
completeCallback has the following signature:
<