Skip to content

Commit df635fc

Browse files
committed
Make sure to call jsonp always as script tag. Fix for #5955 with unit test.
Fixing this bug is simple and for me it won't break any unit tests. The unit test I've added is a bit strange, but worked for me across Firefox 3.6, Chrome and IE6.
1 parent f411c8f commit df635fc

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/ajax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ jQuery.extend({
289289

290290
// If we're requesting a remote document
291291
// and trying to load JSON or Script with a GET
292-
if ( s.dataType === "script" && type === "GET" && remote ) {
292+
if ( s.dataType === "script" && type === "GET" && (remote || jsonp) ) {
293293
var head = document.getElementsByTagName("head")[0] || document.documentElement;
294294
var script = document.createElement("script");
295295
script.src = s.url;

test/data/jsonp.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@
22
error_reporting(0);
33
$callback = $_REQUEST['callback'];
44
$json = $_REQUEST['json'];
5-
if($json) {
5+
$verifyScriptTag = $_REQUEST['verifyScriptTag'];
6+
if($verifyScriptTag) {
7+
?>
8+
window.isScriptTag = false;
9+
(function(){
10+
var i,script,scripts = document.getElementsByTagName('script');
11+
for ( i=0 ; i < scripts.length ; i++ ) { script = scripts[i];
12+
if( (''+script.src).indexOf('verifyScriptTag') != -1 ) {
13+
window.isScriptTag = true;
14+
}
15+
}
16+
})();
17+
<?php
18+
echo $callback . '({ "whatever": "data" })';
19+
} else if($json) {
620
echo $callback . '([ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ])';
721
} else {
822
echo $callback . '({ "data": {"lang": "en", "length": 25} })';

test/unit/ajax.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,8 @@ test("JSONP - Custom JSONP Callback", function() {
770770
});
771771
});
772772

773-
test("jQuery.ajax() - JSONP, Remote", function() {
774-
expect(4);
773+
test("jQuery.ajax() - JSONP, Remote or Local", function() {
774+
expect(5);
775775

776776
var count = 0;
777777
function plus(){ if ( ++count == 4 ) start(); }
@@ -833,6 +833,20 @@ test("jQuery.ajax() - JSONP, Remote", function() {
833833
plus();
834834
}
835835
});
836+
837+
jQuery.ajax({
838+
url: base + "data/jsonp.php",
839+
dataType: "jsonp",
840+
data: {"verifyScriptTag":true},
841+
success: function(data){
842+
ok( window.isScriptTag, "JSONP is script tag" );
843+
// garbage collect
844+
try {
845+
delete window.isScriptTag;
846+
} catch(e) {}
847+
plus();
848+
}
849+
});
836850
});
837851

838852
test("jQuery.ajax() - script, Remote", function() {

0 commit comments

Comments
 (0)