$.ajaxPrefilter( "script", function( options ) { if ( options.cache === undefined ) { options.cache = true; } });
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) { // Your code here });
$.ajaxPrefilter( "json", function( options, originalOptions, jqXHR ) { // Your code for json requests here });
$.ajaxPrefilter( dataTypes, prefilterFN );
function( options, originalOptions, jqXHR ) {}
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) { // Your code here });
$.ajaxPrefilter( "json", function( options, originalOptions, jqXHR ) { // Your code for json requests here });
$.ajaxPrefilter( "script", function( options ) { if ( options.cache === undefined ) { options.cache = true; } });
$.ajaxPrefilter(function( options, _, jqXHR ) { if ( options.log ) { output( "Starting request " + options.url + " " +options.data ); jqXHR.success(function( data ) { output( "Success " + data ); }).error(function( jqXHR, statusText, msg ) { output( "Error " + msg ); }); } });
$.ajax( "srv/echo", { data: { random: 1, delay: 3 }, log: true }); $.ajax( "srv/echo", { data: { random: 1, delay: 10 }, log: true });
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) { if ( options.parseError ) { $.Deferred(function( defer ) { jqXHR.done( defer.resolve ) .fail(function( jqXHR, statusText, errorMsg ) { var parsed = $.parseJSON( jqXHR.responseText ); defer.rejectWith( this, [ jqXHR, statusText, parsed ] ); }); }).promise( jqXHR ); jqXHR.success = jqXHR.done; jqXHR.error = jqXHR.fail; } });
$.ajax( "srv/echo", { parseError: true, data: { error: 1, delay: 1, echo: '{ "field": "value" }' } }).fail(function( jqXHR, statusText, parsed ) { output( parsed.field ); });
$.ajaxSetup({ converters: { // Install microsoft "enhanced" converter "text json": function( msg ) { var obj = Sys.Serialization. JavaScriptSerializer.deserialize( msg ); return obj.hasOwnProperty( "d" ) ? obj.d : obj; } } });
// List of data converters // 1) key format is "source_type destination_type" // (single space in-between) // 2) the catchall symbol "*" can be used for source_type converters: { // Convert anything to text "* text": window.String, // Text to html (true = no transformation) "text html": true, // Evaluate text as a json expression "text json": jQuery.parseJSON, // Parse text as xml "text xml": jQuery.parseXML // Execute a script "text script": function( text ) { jQuery.globalEval( text ); return text; } }
$.ajaxSettings({ converters: { "type1 type2": function( valueType1 ) { // Your logic return valueType2; } } });
$.ajax( url, { converters: { "type1 type2": function( valueType1 ) { // Your logic return valueType2; } } });
$.ajax( "srv/echo", { dataType: "json", data: { delay: 3, random: 1 }, converters: { "text json": function( _ ) { throw "DO NOT WANT"; } } }).error(function( jqXHR, statusText, error ) { output( statusText, error ); });
$.ajaxSetup({ converters: { // Install microsoft "enhanced" converter "text json": function( msg ) { var obj = Sys.Serialization. JavaScriptSerializer.deserialize( msg ); return obj.hasOwnProperty( "d" ) ? obj.d : obj; } } });
$.ajaxSetup({ converters: { // Install microsoft "enhanced" converter "text json": function( msg ) { return Sys.Serialization.JavaScriptSerializer.deserialize( msg ); }, // Install new dataType "json jsond": function( json ) { return json && json.hasOwnProperty( "d" ) ? json.d : json; } } });
$.ajax( url, { dataType: "jsond" });
$.ajax( url, { dataType: "jsonp jsond" });
$.ajaxPrefilter(function( options ) { // If custom msjson option is true if ( options.msService ) { jQuery.ajaxSetup( options, { converters: { // Install microsoft based converter "text json": function( msg ) { var obj = Sys.Serialization. JavaScriptSerializer.deserialize( msg ); return obj.hasOwnProperty( "d" ) ? obj.d : obj; } }, // Serialize data using microsoft serializer data: Sys.Serialization. JavaScriptSerializer.serialize( options.data ), // Force type and contentType type: "POST", contentType: "application/json; charset=utf-8" }); } });
$.ajax( url, { msService: true, // Awesomesauce! data: { "hello": "world" } });
$.ajaxSetup({ msService: true }); $.post( url, { data: { "hello": "world" } });
{ send: function( requestHeaders, doneFN ) { // Initiate the request here }, abort: function() { // Provide a mean to abort the request here } }
send: function( requestHeaders, doneFN ) {}
{ text: rawText, xml: parsedText }
abort()
$.ajaxTransport(function( options, originalOptions, jqXHR ) { if ( /* I can handle the request */ ) { return { send: function( requestHeaders, doneFN ) { // Initiate the request here }, abort: function() { // Provide a mean to abort the request here } }; } });
$.ajaxTransport( dataTypes, transportFactory );
$.ajaxTransport( "img", function( options ) { if ( options.type === "GET" ) { var image; return { send: function( _, callback ) { image = new Image(); image.onload = function() { image = image.onload = image.onerror = null; callback( 200, "OK", { img: this } ); }; image.onerror = function( abort ) { image = image.onload = image.onerror = null; callback( abort === "abort" ? 0 : 404, abort === "abort" ? "abort" : "Not Found" ); }; image.src = options.url; }, abort: function() { if ( image ) image.onerror( "abort" ); } }; } });