]> source.dussan.org Git - jquery.git/commitdiff
Fixes #4897. Added ?? as a context-insensitive placeholder for the callback name...
authorjaubourg <j@ubourg.net>
Wed, 12 Jan 2011 17:36:00 +0000 (18:36 +0100)
committerjaubourg <j@ubourg.net>
Wed, 12 Jan 2011 17:36:00 +0000 (18:36 +0100)
src/ajax/jsonp.js
test/data/jsonp.php
test/unit/ajax.js

index f4b324e17c2155308e0b880598035b99f55be9eb..dd04b34b02c90bbe9de1402458ff8eaef28e776b 100644 (file)
@@ -1,7 +1,7 @@
 (function( jQuery ) {
 
 var jsc = jQuery.now(),
-       jsre = /\=(?:\?|%3F)(&|$)/i,
+       jsre = /(\=)(?:\?|%3F)(&|$)|()(?:\?\?|%3F%3F)()/i,
        rquery_jsonp = /\?/;
 
 // Default jsonp settings
@@ -25,8 +25,8 @@ jQuery.ajax.prefilter("json jsonp", function(s, originalSettings) {
 
                var jsonpCallback = s.jsonpCallback =
                                jQuery.isFunction( s.jsonpCallback ) ? s.jsonpCallback() : s.jsonpCallback,
-                       url = s.url.replace(jsre, "=" + jsonpCallback + "$1"),
-                       data = s.url === url && typeof(s.data) === "string" ? s.data.replace(jsre, "=" + jsonpCallback + "$1") : s.data;
+                       url = s.url.replace(jsre, "$1" + jsonpCallback + "$2"),
+                       data = s.url === url && typeof(s.data) === "string" ? s.data.replace(jsre, "$1" + jsonpCallback + "$2") : s.data;
 
                if ( url === s.url && data === s.data ) {
                        url += (rquery_jsonp.test( url ) ? "&" : "?") + s.jsonp + "=" + jsonpCallback;
index 9ae1d84872bca498040b2e71ad157bcf475fc2fd..6c13d72e9d2971b5ec8e70bfc94b45ad4f92f030 100644 (file)
@@ -1,6 +1,10 @@
 <?php
 error_reporting(0);
 $callback = $_REQUEST['callback'];
+if ( ! $callback ) {
+       $callback = explode("?",end(explode("/",$_SERVER['REQUEST_URI'])));
+       $callback = $callback[0];
+}
 $json = $_REQUEST['json'];
 if($json) {
        echo $callback . '([ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ])';
index 4162d585a133a426a0e3923a615a98056b9b2eb8..10c2e71837837dd6d6efaf201fca33c1ab8b8cdc 100644 (file)
@@ -1115,10 +1115,10 @@ test("jQuery.getScript(String, Function) - no callback", function() {
 });
 
 test("jQuery.ajax() - JSONP, Local", function() {
-       expect(10);
+       expect(14);
 
        var count = 0;
-       function plus(){ if ( ++count == 10 ) start(); }
+       function plus(){ if ( ++count == 14 ) start(); }
 
        stop();
 
@@ -1162,6 +1162,59 @@ test("jQuery.ajax() - JSONP, Local", function() {
                }
        });
 
+       jQuery.ajax({
+               url: "data/jsonp.php?callback=??",
+               dataType: "jsonp",
+               success: function(data){
+                       ok( data.data, "JSON results returned (GET, url context-free callback)" );
+                       plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, url context-free callback)" );
+                       plus();
+               }
+       });
+
+       jQuery.ajax({
+               url: "data/jsonp.php",
+               dataType: "jsonp",
+               data: "callback=??",
+               success: function(data){
+                       ok( data.data, "JSON results returned (GET, data context-free callback)" );
+                       plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, data context-free callback)" );
+                       plus();
+               }
+       });
+
+       jQuery.ajax({
+               url: "data/jsonp.php/??",
+               dataType: "jsonp",
+               success: function(data){
+                       ok( data.data, "JSON results returned (GET, REST-like)" );
+                       plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, REST-like)" );
+                       plus();
+               }
+       });
+
+       jQuery.ajax({
+               url: "data/jsonp.php/???json=1",
+               dataType: "jsonp",
+               success: function(data){
+                       strictEqual( jQuery.type(data), "array", "JSON results returned (GET, REST-like with param)" );
+                       plus();
+               },
+               error: function(data){
+                       ok( false, "Ajax error JSON (GET, REST-like with param)" );
+                       plus();
+               }
+       });
+
        jQuery.ajax({
                url: "data/jsonp.php",
                dataType: "jsonp",