Skip to content

Commit a2ae215

Browse files
committed
Ajax: Remove jsonp callbacks through "jQuery#removeProp" method
Fixes gh-2323 Closes gh-2464
1 parent 3ec73ef commit a2ae215

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/ajax/jsonp.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
6464

6565
// Clean-up function (fires after converters)
6666
jqXHR.always(function() {
67-
// Restore preexisting value
68-
window[ callbackName ] = overwritten;
67+
// If previous value didn't exist - remove it
68+
if ( overwritten === undefined ) {
69+
jQuery( window ).removeProp( callbackName );
70+
71+
// Otherwise restore preexisting value
72+
} else {
73+
window[ callbackName ] = overwritten;
74+
}
6975

7076
// Save back as free
7177
if ( s[ callbackName ] ) {

test/unit/ajax.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
module( "ajax", {
2-
setup: function() {
3-
var jsonpCallback = this.jsonpCallback = jQuery.ajaxSettings.jsonpCallback;
4-
jQuery.ajaxSettings.jsonpCallback = function() {
5-
var callback = jsonpCallback.apply( this, arguments );
6-
Globals.register( callback );
7-
return callback;
8-
};
9-
},
102
teardown: function() {
113
jQuery( document ).off( "ajaxStart ajaxStop ajaxSend ajaxComplete ajaxError ajaxSuccess" );
124
moduleTeardown.apply( this, arguments );
@@ -742,7 +734,7 @@ module( "ajax", {
742734
}
743735
]);
744736

745-
ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 9, {
737+
ajaxTest( "jQuery.ajax() - JSONP - Explicit callback param" + label, 10, {
746738
setup: function() {
747739
Globals.register("functionToCleanUp");
748740
Globals.register("XXX");
@@ -765,6 +757,11 @@ module( "ajax", {
765757
crossDomain: crossDomain,
766758
jsonpCallback: "jsonpResults",
767759
success: function( data ) {
760+
strictEqual(
761+
typeof window[ "jsonpResults" ],
762+
"function",
763+
"should not rewrite original function"
764+
);
768765
ok( data.data, "JSON results returned (GET, custom callback name)" );
769766
}
770767
}, {
@@ -1356,16 +1353,29 @@ module( "ajax", {
13561353
]);
13571354

13581355
jQuery.each( [ " - Same Domain", " - Cross Domain" ], function( crossDomain, label ) {
1359-
ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 2, {
1356+
ajaxTest( "#8205 - jQuery.ajax() - JSONP - re-use callbacks name" + label, 4, {
13601357
url: "data/jsonp.php",
13611358
dataType: "jsonp",
13621359
crossDomain: crossDomain,
13631360
beforeSend: function( jqXHR, s ) {
13641361
s.callback = s.jsonpCallback;
1362+
1363+
ok( this.callback in window, "JSONP callback name is in the window" );
13651364
},
13661365
success: function() {
13671366
var previous = this;
1368-
strictEqual( previous.jsonpCallback, undefined, "jsonpCallback option is set back to default in callbacks" );
1367+
1368+
strictEqual(
1369+
previous.jsonpCallback,
1370+
undefined,
1371+
"jsonpCallback option is set back to default in callbacks"
1372+
);
1373+
1374+
ok(
1375+
!( this.callback in window ),
1376+
"JSONP callback name was removed from the window"
1377+
);
1378+
13691379
jQuery.ajax({
13701380
url: "data/jsonp.php",
13711381
dataType: "jsonp",

0 commit comments

Comments
 (0)