🌐 AI搜索 & 代理 主页
Skip to content

Commit fbc73d4

Browse files
committed
Added in support for $.ajax jsonpCallback (allowing you to specify the name of the callback method - and allowing you to avoid skipping the cache). Fixes #4206.
1 parent aea5b09 commit fbc73d4

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/ajax.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ jQuery.extend({
221221

222222
// Build temporary JSONP function
223223
if ( s.dataType === "json" && (s.data && jsre.test(s.data) || jsre.test(s.url)) ) {
224-
jsonp = "jsonp" + jsc++;
224+
jsonp = s.jsonpCallback || ("jsonp" + jsc++);
225225

226226
// Replace the =? sequence both in the query string and the data
227227
if ( s.data ) {
@@ -235,7 +235,7 @@ jQuery.extend({
235235
s.dataType = "script";
236236

237237
// Handle JSONP-style loading
238-
window[ jsonp ] = function(tmp){
238+
window[ jsonp ] = window[ jsonp ] || function(tmp){
239239
data = tmp;
240240
success();
241241
complete();

test/unit/ajax.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,10 @@ test("jQuery.getScript(String, Function) - no callback", function() {
518518
});
519519

520520
test("jQuery.ajax() - JSONP, Local", function() {
521-
expect(7);
521+
expect(8);
522522

523523
var count = 0;
524-
function plus(){ if ( ++count == 7 ) start(); }
524+
function plus(){ if ( ++count == 8 ) start(); }
525525

526526
stop();
527527

@@ -579,6 +579,20 @@ test("jQuery.ajax() - JSONP, Local", function() {
579579
}
580580
});
581581

582+
jQuery.ajax({
583+
url: "data/jsonp.php",
584+
dataType: "jsonp",
585+
jsonpCallback: "jsonpResults",
586+
success: function(data){
587+
ok( data.data, "JSON results returned (GET, custom callback name)" );
588+
plus();
589+
},
590+
error: function(data){
591+
ok( false, "Ajax error JSON (GET, custom callback name)" );
592+
plus();
593+
}
594+
});
595+
582596
jQuery.ajax({
583597
type: "POST",
584598
url: "data/jsonp.php",
@@ -624,6 +638,22 @@ test("jQuery.ajax() - JSONP, Local", function() {
624638
});
625639
});
626640

641+
test("JSONP - Custom JSONP Callback", function() {
642+
expect(1);
643+
stop();
644+
645+
window.jsonpResults = function(data) {
646+
ok( data.data, "JSON results returned (GET, custom callback function)" );
647+
start();
648+
};
649+
650+
jQuery.ajax({
651+
url: "data/jsonp.php",
652+
dataType: "jsonp",
653+
jsonpCallback: "jsonpResults"
654+
});
655+
});
656+
627657
test("jQuery.ajax() - JSONP, Remote", function() {
628658
expect(4);
629659

0 commit comments

Comments
 (0)