]> source.dussan.org Git - jquery.git/commitdiff
Ajax: Remove jsonp callbacks through "jQuery#removeProp" method
authorOleg Gaidarenko <markelog@gmail.com>
Fri, 10 Jul 2015 17:58:43 +0000 (20:58 +0300)
committerOleg Gaidarenko <markelog@gmail.com>
Sun, 12 Jul 2015 22:34:09 +0000 (01:34 +0300)
Fixes gh-2323
Closes gh-2464

src/ajax/jsonp.js
test/unit/ajax.js

index a04898f67def913a82fbd191db7ff191e798ba57..f469344e0b573fd3123c48b51a97753b3bef93d2 100644 (file)
@@ -64,8 +64,14 @@ jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
 
                // Clean-up function (fires after converters)
                jqXHR.always(function() {
-                       // Restore preexisting value
-                       window[ callbackName ] = overwritten;
+                       // If previous value didn't exist - remove it
+                       if ( overwritten === undefined ) {
+                               jQuery( window ).removeProp( callbackName );
+
+                       // Otherwise restore preexisting value
+                       } else {
+                               window[ callbackName ] = overwritten;
+                       }
 
                        // Save back as free
                        if ( s[ callbackName ] ) {
index 8a4ca7a657690aeb5c3208b5bbb9228e2e274227..359fa9d8193bf44a4929d6ebb92971ad9c3068ee 100644 (file)
@@ -1,12 +1,4 @@
 module( "ajax", {
-       setup: function() {
-               var jsonpCallback = this.jsonpCallback = jQuery.ajaxSettings.jsonpCallback;
-               jQuery.ajaxSettings.jsonpCallback = function() {
-                       var callback = jsonpCallback.apply( this, arguments );
-                       Globals.register( callback );
-                       return callback;
-               };
-       },
        teardown: function() {
                jQuery( document ).off( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess" );
                moduleTeardown.apply( this, arguments );
@@ -742,7 +734,7 @@ module( "ajax", {
                        }
                ]);
 
-               ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 9, {
+               ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 10, {
                        setup: function() {
                                Globals.register("functionToCleanUp");
                                Globals.register("XXX");
@@ -765,6 +757,11 @@ module( "ajax", {
                                crossDomain: crossDomain,
                                jsonpCallback: "jsonpResults",
                                success: function( data ) {
+                                       strictEqual(
+                                               typeof window[ "jsonpResults" ],
+                                               "function",
+                                               "should not rewrite original function"
+                                       );
                                        ok( data.data, "JSON results returned (GET, custom callback name)" );
                                }
                        }, {
@@ -1356,16 +1353,29 @@ module( "ajax", {
        ]);
 
        jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
-               ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 2, {
+               ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 4, {
                        url: "data/jsonp.php",
                        dataType: "jsonp",
                        crossDomain: crossDomain,
                        beforeSend: function( jqXHR, s ) {
                                s.callback = s.jsonpCallback;
+
+                               ok( this.callback in window, "JSONP callback name is in the window" );
                        },
                        success: function() {
                                var previous = this;
-                               strictEqual( previous.jsonpCallback, undefined, "jsonpCallback option is set back to default in callbacks" );
+
+                               strictEqual(
+                                       previous.jsonpCallback,
+                                       undefined,
+                                       "jsonpCallback option is set back to default in callbacks"
+                               );
+
+                               ok(
+                                       !( this.callback in window ),
+                                       "JSONP callback name was removed from the window"
+                               );
+
                                jQuery.ajax({
                                        url: "data/jsonp.php",
                                        dataType: "jsonp",