From: jaubourg Date: Wed, 9 Feb 2011 16:47:33 +0000 (+0100) Subject: Fixes #8219. Introduces the mimeType option to override content-type header in conver... X-Git-Url: http://git.asbjorn.biz/?a=commitdiff_plain;ds=sidebyside;h=f6e173437e9f94cd4e713e556c6fc8ca68be8384;p=jquery.git Fixes #8219. Introduces the mimeType option to override content-type header in conversion (and in native xhr when possible). Adds companion overrideMimeType method on jqXHR object (it simply sets the option). Unit test added. --- diff --git a/src/ajax.js b/src/ajax.js index 15ad6a8..5a6ac26 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -426,6 +426,14 @@ jQuery.extend({ return match || null; }, + // Overrides response content-type header + overrideMimeType: function( type ) { + if ( state === 0 ) { + s.mimeType = type; + } + return this; + }, + // Cancel the request abort: function( statusText ) { statusText = statusText || "abort"; @@ -827,7 +835,7 @@ function ajaxHandleResponses( s, jqXHR, responses ) { while( dataTypes[ 0 ] === "*" ) { dataTypes.shift(); if ( ct === undefined ) { - ct = jqXHR.getResponseHeader( "content-type" ); + ct = s.mimeType || jqXHR.getResponseHeader( "content-type" ); } } diff --git a/src/ajax/xhr.js b/src/ajax/xhr.js index 1f136c3..fcea52c 100644 --- a/src/ajax/xhr.js +++ b/src/ajax/xhr.js @@ -87,6 +87,11 @@ if ( jQuery.support.ajax ) { } } + // Override mime type if needed + if ( s.mimeType && xhr.overrideMimeType ) { + xhr.overrideMimeType( s.mimeType ); + } + // Requested-With header // Not set for crossDomain requests with no content // (see why at http://trac.dojotoolkit.org/ticket/9486) diff --git a/test/unit/ajax.js b/test/unit/ajax.js index 59159c3..74c6545 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -2169,6 +2169,34 @@ test("jQuery.ajax - transitive conversions", function() { }); +test("jQuery.ajax - overrideMimeType", function() { + + expect( 2 ); + + stop(); + + jQuery.when( + + jQuery.ajax( url("data/json.php") , { + beforeSend: function( xhr ) { + xhr.overrideMimeType( "application/json" ); + }, + success: function( json ) { + ok( json.data , "Mimetype overriden using beforeSend" ); + } + }), + + jQuery.ajax( url("data/json.php") , { + mimeType: "application/json", + success: function( json ) { + ok( json.data , "Mimetype overriden using mimeType option" ); + } + }) + + ).then( start , start ); + +}); + test("jQuery.ajax - abort in prefilter", function() { expect( 1 );