]> source.dussan.org Git - jquery.git/commitdiff
Fix #14503: Cast to string before setting XHR header. Close gh-1427.
authorhongymagic <d.hong@me.com>
Mon, 11 Nov 2013 02:28:36 +0000 (13:28 +1100)
committerRichard Gibson <richard.gibson@gmail.com>
Fri, 15 Nov 2013 01:32:56 +0000 (20:32 -0500)
src/ajax/xhr.js
test/data/headers.php
test/unit/ajax.js

index fe24b67515241efcbea1646bca651322344dcc6a..d747093d51bc0a8841bd3d5cd31c5afdbc55f3a1 100644 (file)
@@ -74,7 +74,15 @@ if ( xhrSupported ) {
 
                                        // Set headers
                                        for ( i in headers ) {
-                                               xhr.setRequestHeader( i, headers[ i ] );
+                                               // Support: IE<9
+                                               // IE's ActiveXObject throws a 'Type Mismatch' exception when setting
+                                               // request header to a null-value.
+                                               //
+                                               // To keep consistent with other XHR implementations, cast the value
+                                               // to string and ignore `undefined`.
+                                               if ( headers[ i ] !== undefined ) {
+                                                       xhr.setRequestHeader( i, headers[ i ] + "" );
+                                               }
                                        }
 
                                        // Do send the request
index 968f13f19abc487dcc2bfa4c7a03603089ad4690..79c18305515bf8315f163cde5652376f951ec467 100644 (file)
@@ -14,5 +14,10 @@ foreach( $_SERVER as $key => $value ) {
 }
 
 foreach( explode( "_" , $_GET[ "keys" ] ) as $key ) {
-       echo "$key: " . @$headers[ strtoupper( $key ) ] . "\n";
+
+       // Only echo if key exists in the header
+       if ( isset( $headers[ strtoupper( $key ) ] ) ) {
+               echo "$key: " . @$headers[ strtoupper( $key ) ] . "\n";
+       }
+
 }
index fee2d8a04730fb3076f45ee570160d1f4a1e1763..6166e7bef7c1242ef4adead36d796c47c7d3344b 100644 (file)
@@ -177,17 +177,24 @@ module( "ajax", {
                });
        });
 
-       ajaxTest( "jQuery.ajax() - headers", 4, {
+       ajaxTest( "jQuery.ajax() - headers", 5, {
                setup: function() {
                        jQuery( document ).ajaxSend(function( evt, xhr ) {
                                xhr.setRequestHeader( "ajax-send", "test" );
                        });
                },
-               url: url("data/headers.php?keys=siMPle_SometHing-elsE_OthEr_ajax-send"),
+               url: url("data/headers.php?keys=siMPle_SometHing-elsE_OthEr_Nullable_undefined_Empty_ajax-send"),
                headers: {
                        "siMPle": "value",
                        "SometHing-elsE": "other value",
-                       "OthEr": "something else"
+                       "OthEr": "something else",
+                       "Nullable": null,
+                       "undefined": undefined,
+
+                       // Support: Firefox
+                       // Not all browsers allow empty-string headers
+                       // https://bugzilla.mozilla.org/show_bug.cgi?id=815299
+                       //"Empty": ""
                },
                success: function( data, _, xhr ) {
                        var i, emptyHeader,
@@ -196,12 +203,13 @@ module( "ajax", {
                                }),
                                tmp = [];
                        for ( i in requestHeaders ) {
-                               tmp.push( i, ": ", requestHeaders[ i ], "\n" );
+                               tmp.push( i, ": ", requestHeaders[ i ] + "", "\n" );
                        }
                        tmp = tmp.join("");
 
                        strictEqual( data, tmp, "Headers were sent" );
                        strictEqual( xhr.getResponseHeader("Sample-Header"), "Hello World", "Sample header received" );
+                       ok( data.indexOf( "undefined" ) < 0 , "Undefined header value was not sent" );
 
                        emptyHeader = xhr.getResponseHeader("Empty-Header");
                        if ( emptyHeader === null ) {
@@ -238,7 +246,7 @@ module( "ajax", {
                        url: url("data/headers.php?keys=content-type"),
                        contentType: false,
                        success: function( data ) {
-                               strictEqual( data, "content-type: \n", "Test content-type is not sent when options.contentType===false" );
+                               strictEqual( data, "", "Test content-type is not sent when options.contentType===false" );
                        }
                }
        ]);