- // Set header so the called script knows that it's an XMLHttpRequest
- // Only send the header if it's not a remote XHR
- if ( !remote ) {
- xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
- }
-
- // Set the Accepts header for the server, depending on the dataType
- xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?
- s.accepts[ s.dataType ] + ", */*" :
- s.accepts._default );
- } catch(e) {}
-
- // Allow custom headers/mimetypes and early abort
- if ( s.beforeSend && s.beforeSend.call(callbackContext, xhr, s) === false ) {
- // Handle the global AJAX counter
- if ( s.global && ! --jQuery.active ) {
- jQuery.event.trigger( "ajaxStop" );
- }
-
- // close opended socket
- xhr.abort();
- return false;
- }
-
- if ( s.global ) {
- trigger("ajaxSend", [xhr, s]);
- }
-
- // Wait for a response to come back
- var onreadystatechange = xhr.onreadystatechange = function( isTimeout ) {
- // The request was aborted, clear the interval and decrement jQuery.active
- if ( !xhr || xhr.readyState === 0 ) {
- requestDone = true;
- xhr.onreadystatechange = jQuery.noop;
-
- // Handle the global AJAX counter
- if ( s.global && ! --jQuery.active ) {
- jQuery.event.trigger( "ajaxStop" );
- }
-
- // The transfer is complete and the data is available, or the request timed out
- } else if ( !requestDone && xhr && (xhr.readyState === 4 || isTimeout === "timeout") ) {
- requestDone = true;
- xhr.onreadystatechange = jQuery.noop;
-
- status = isTimeout === "timeout" ?
- "timeout" :
- !jQuery.httpSuccess( xhr ) ?
- "error" :
- s.ifModified && jQuery.httpNotModified( xhr, s.url ) ?
- "notmodified" :
- "success";
-
- if ( status === "success" ) {
- // Watch for, and catch, XML document parse errors
- try {
- // process the data (runs the xml through httpData regardless of callback)
- data = jQuery.httpData( xhr, s.dataType, s );
- } catch(e) {
- status = "parsererror";
- }
- }
-
- // Make sure that the request was successful or notmodified
- if ( status === "success" || status === "notmodified" ) {
- // JSONP handles its own success callback
- if ( !jsonp ) {
- success();
- }
- } else {
- jQuery.handleError(s, xhr, status);
- }
-
- // Fire the complete handlers
- complete();
-
- if ( isTimeout === "timeout" ) {
- xhr.abort();
- }
-
- // Stop memory leaks
- if ( s.async ) {
- xhr = null;
- }
- }
- };
-
- // Timeout checker
- if ( s.async && s.timeout > 0 ) {
- setTimeout(function() {
- // Check to see if the request is still happening
- if ( xhr && !requestDone ) {
- onreadystatechange( "timeout" );
+ // List of data converters
+ // 1) key format is "source_type => destination_type" (spaces required)
+ // 2) the catchall symbol "*" can be used for source_type
+ dataConverters: {
+
+ // Convert anything to text
+ "* => text": function(data) {
+ return "" + data;
+ },
+
+ // Text to html (no transformation)
+ "text => html": function(data) {
+ return data;
+ },
+
+ // Evaluate text as a json expression
+ "text => json": jQuery.parseJSON,
+
+ // Parse text as xml
+ "text => xml": function(data) {
+ var xml, parser;
+ if ( window.DOMParser ) { // Standard
+ parser = new DOMParser();
+ xml = parser.parseFromString(data,"text/xml");
+ } else { // IE
+ xml = new ActiveXObject("Microsoft.XMLDOM");
+ xml.async="false";
+ xml.loadXML(data);