]> source.dussan.org Git - jquery.git/commitdiff
Fixes #8219. Introduces the mimeType option to override content-type header in conver...
authorjaubourg <j@ubourg.net>
Wed, 9 Feb 2011 16:47:33 +0000 (17:47 +0100)
committerjaubourg <j@ubourg.net>
Wed, 9 Feb 2011 16:47:33 +0000 (17:47 +0100)
src/ajax.js
src/ajax/xhr.js
test/unit/ajax.js

index 15ad6a85c519753fcadf356146fef73a2c351d72..5a6ac26ad67d3245e70ef6e0e563d957b3ff4986 100644 (file)
@@ -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" );
                }
        }
 
index 1f136c343ed6e690136fee095f602a0afbacb729..fcea52c16c6ecedf9b07d34fb22b2ba45a9b8430 100644 (file)
@@ -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)
index 59159c320734cfcdfebfd374c5ae831166e0ab67..74c6545c5cb9ce6f7d4fb3e5b44a8f192c55d432 100644 (file)
@@ -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 );