Fixes #8219. Introduces the mimeType option to override content-type header in conver...
[jquery.git] / src / ajax.js
index 4830e3d..5a6ac26 100644 (file)
@@ -365,9 +365,10 @@ jQuery.extend({
                                ( s.context = ( "context" in options ? options : jQuery.ajaxSettings ).context ) || s,
                        // Context for global events
                        // It's the callbackContext if one was provided in the options
-                       // and if it's a DOM node
-                       globalEventContext = callbackContext !== s && callbackContext.nodeType ?
-                               jQuery( callbackContext ) : jQuery.event,
+                       // and if it's a DOM node or a jQuery collection
+                       globalEventContext = callbackContext !== s &&
+                               ( callbackContext.nodeType || callbackContext instanceof jQuery ) ?
+                                               jQuery( callbackContext ) : jQuery.event,
                        // Deferreds
                        deferred = jQuery.Deferred(),
                        completeDeferred = jQuery._Deferred(),
@@ -425,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";
@@ -508,7 +517,7 @@ jQuery.extend({
                                // We extract error from statusText
                                // then normalize statusText and status for non-aborts
                                error = statusText;
-                               if( status ) {
+                               if( !statusText || status ) {
                                        statusText = "error";
                                        if ( status < 0 ) {
                                                status = 0;
@@ -684,8 +693,7 @@ jQuery.extend({
                if ( !transport ) {
                        done( -1, "No Transport" );
                } else {
-                       // Set state as sending
-                       state = jqXHR.readyState = 1;
+                       jqXHR.readyState = 1;
                        // Send global event
                        if ( fireGlobals ) {
                                globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
@@ -698,6 +706,7 @@ jQuery.extend({
                        }
 
                        try {
+                               state = 1;
                                transport.send( requestHeaders, done );
                        } catch (e) {
                                // Propagate exception as error if not done
@@ -826,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" );
                }
        }