{"name":"jQuery.ajaxTransport","type":"method","title":"jQuery.ajaxTransport()","deprecated":null,"removed":null,"desc":"Creates an object that handles the actual transmission of Ajax data.","categories":["ajax/low-level-interface","version/1.5"],"entries":[{"return":"undefined","signatures":{"added":"1.5","argument":[{"desc":"A string identifying the data type to use","name":"dataType","type":"String"},{"desc":"A handler to return the new transport object to use with the data type provided in the first argument.","name":"handler(options, originalOptions, jqXHR)","type":"Function"}]},"examples":null,"longdesc":"\n

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.

\n

Since each request requires its own transport object instance, transports cannot be registered directly. Therefore, you should provide a function that returns a transport instead.

\n

Transports factories are registered using $.ajaxTransport(). A typical registration looks like this:

\n
\n$.ajaxTransport(function( options, originalOptions, jqXHR ) {\n  if( /* transportCanHandleRequest */ ) {\n    return {\n      send: function( headers, completeCallback ) {\n        // Send code\n      },\n      abort: function() {\n        // Abort code\n      }\n    };\n  }\n});\n    
\n

where:

\n \n

completeCallback has the following signature:

\n
\nfunction( status, statusText, responses, headers ) {}\n    
\n

where:

\n \n

Just like prefilters, a transport's factory function can be attached to a specific dataType:

\n
\n$.ajaxTransport( \"script\", function( options, originalOptions, jqXHR ) {\n  // Will only be called for script requests\n});\n    
\n

The following example shows how a minimal image transport could be implemented:

\n
\n$.ajaxTransport( \"image\", function( s ) {\n  if ( s.type === \"GET\" && s.async ) {\n    var image;\n    return {\n      send: function( _ , callback ) {\n        image = new Image();\n        function done( status ) {\n          if ( image ) {\n            var statusText = ( status === 200 ) ? \"success\" : \"error\",\n              tmp = image;\n            image = image.onreadystatechange = image.onerror = image.onload = null;\n            callback( status, statusText, { image: tmp } );\n          }\n        }\n        image.onreadystatechange = image.onload = function() {\n          done( 200 );\n        };\n        image.onerror = function() {\n          done( 404 );\n        };\n        image.src = s.url;\n      },\n      abort: function() {\n        if ( image ) {\n          image = image.onreadystatechange = image.onerror = image.onload = null;\n        }\n      }\n    };\n  }\n});\n    
\n

Handling Custom Data Types

\n

The jQuery Ajax implementation comes with a set of standard dataTypes, such as text, json, xml, and html.

\n

Use the converters option in $.ajaxSetup() to augment or modify the data type conversion strategies used by $.ajax().

\n

The unminified jQuery source itself includes a list of default converters, which effectively illustrates how they can be used:

\n
\n// List of data converters\n// 1) Key format is \"source_type destination_type\"\n//    (a single space in-between)\n// 2) The catchall symbol \"*\" can be used for source_type\nconverters: {\n  // Convert anything to text\n  \"* text\": window.String,\n  // Text to html (true = no transformation)\n  \"text html\": true,\n  // Evaluate text as a json expression\n  \"text json\": jQuery.parseJSON,\n  // Parse text as xml\n  \"text xml\": jQuery.parseXML\n}\n    
\n

When you specify a converters option globally in $.ajaxSetup() or per call in $.ajax(), the object will map onto the default converters, overwriting those you specify and leaving the others intact.

\n

For example, the jQuery source uses $.ajaxSetup() to add a converter for \"text script\":

\n
\njQuery.ajaxSetup({\n  accepts: {\n    script: \"text/javascript, application/javascript\"\n  },\n  contents: {\n    script: /javascript/\n  },\n  converters: {\n    \"text script\": jQuery.globalEval\n  }\n});\n    
\n "}]}