Skip to content
Browse files

Fix detecting whether JSONP was requested via query param

If `=?` characters appeared in the URL, Zepto would automatically go
into JSONP mode. However, this overly eager detection bites back when
the actual path of the URL ends with `=` character, e.g. when the URL
contains base64-encoded data.

This changes the detection so that `=?` only trigger JSONP mode if they
appear after another `?` character, to ensure they are indeed contained
in the query string portion of the URL.

Fixes #903
  • Loading branch information...
1 parent 4c456f6 commit 9b03ff00fb7bd57668fb89c2e479d575024c5c68 @mislav mislav committed
Showing with 16 additions and 3 deletions.
  1. +2 −2 src/ajax.js
  2. +13 −0 test/ajax.html
  3. +1 −1 test/server.coffee
View
4 src/ajax.js
@@ -113,7 +113,7 @@
responseData = arguments
}
- script.src = options.url.replace(/=\?/, '=' + callbackName)
+ script.src = options.url.replace(/\?(.+)=\?/, '?$1=' + callbackName)
document.head.appendChild(script)
if (options.timeout > 0) abortTimeout = setTimeout(function(){
@@ -196,7 +196,7 @@
serializeData(settings)
if (settings.cache === false) settings.url = appendQuery(settings.url, '_=' + Date.now())
- var dataType = settings.dataType, hasPlaceholder = /=\?/.test(settings.url)
+ var dataType = settings.dataType, hasPlaceholder = /\?.+=\?/.test(settings.url)
if (dataType == 'jsonp' || hasPlaceholder) {
if (!hasPlaceholder)
settings.url = appendQuery(settings.url,
View
13 test/ajax.html
@@ -338,6 +338,19 @@
})
},
+ testAjaxEqualQuestionmarkIsNotAlwaysJSONP: function(t){
+ t.pause()
+ resumeOnAjaxError(t)
+ $.ajax({
+ url: 'echo=?',
+ dataType: 'text',
+ success: t.reg.resumeHandler('success', function(data){
+ t.assertLine('GET ?{}', data)
+ t.assertLine("accept: text/plain", data)
+ })
+ })
+ },
+
testAjaxGetJSONP: function(t){
t.pause()
$.getJSON(
View
2 test/server.coffee
@@ -50,7 +50,7 @@ browser = (ua) ->
app.all '/', (req, res) ->
res.redirect '/test'
-app.all '/test/echo', (req, res) ->
+app.all '/test/echo=?', (req, res) ->
res.set 'Cache-Control', 'no-cache'
res.send """
#{req.method} ?#{dump(req.query)}

0 comments on commit 9b03ff0

Please sign in to comment.
Something went wrong with that request. Please try again.