Skip to main content

Bug Tracker

Side navigation

#2020 closed bug (fixed)

Opened December 06, 2007 04:34PM UTC

Closed December 19, 2007 02:44AM UTC

Last modified March 15, 2012 07:29PM UTC

Data containing ? at the start getting converted to jsonp.... by ajax() call

Reported by: knobunc Owned by: john
Priority: major Milestone: 1.2.2
Component: ajax Version: 1.2.1
Keywords: ajax jsonp ? json Cc:
Blocked by: Blocking:
Description

Using jQuery 1.2.1, if I have:

$.ajax({

type: "POST",

url: "test.html",

dataType: "json",

data: {query: queryString},

});

When queryString starts with a ? it will get converted to

jsonp1231234124...

This is clearly happening because of the code at line 2226 of the full

release version:

Build temporary JSONP function

if ( s.dataType == "json" && (s.data &&

s.data.match(jsre) || s.url.match(jsre)) ) {

jsonp = "jsonp" + jsc++;

Replace the =? sequence both in the query

string and the data

if ( s.data )

s.data = s.data.replace(jsre, "=" +

jsonp);

s.url = s.url.replace(jsre, "=" + jsonp);

...

But I see no way to prevent that from happening.

Now... one might suggest that I should avoid a leading ? in my option names. But I am taking them from input boxes and ? is a valid thing for a user to type. Unfortunately there seems to be no good way to escape the string to prevent this behavior (without teaching the called code how to unescape it).

Also, the docs don't mention that the =? escaping happens to a json

dataType... I see it for jsonp.

Is this behavior intentional? If so, there should be a way to

suppress it or at a way for the calling code to escape the values to

cause a leading ? to be passed to the server.

This behavior is present in the latest svn version of the code too:

http://dev.jquery.com/browser/trunk/jquery/src/ajax.js

But not in releases preceeding 1.2.

Attachments (2)
  • 2020-2.diff (1.3 KB) - added by john December 17, 2007 12:33AM UTC.

    My stab at it.

  • 2020.diff (1.0 KB) - added by davidserduke December 17, 2007 12:01AM UTC.

    patch

Change History (6)

Changed December 11, 2007 04:53PM UTC by john comment:1

owner: → john

Changed December 17, 2007 12:44AM UTC by john comment:2

resolution: → fixed
status: newclosed

Committed in SVN rev [4192].

Changed December 18, 2007 05:33PM UTC by knobunc comment:3

resolution: fixed
status: closedreopened

I'm not certain this fix is correct. I think that this fixes the bug I reported, but breaks the jsonp behavior if they do in:

$.ajax(
    type:     "POST",
    url:      "test.html",
    dataType: "jsonp",
    data:     {query:    "asd",
               callback: "?"},
});

By my reading, it's not going to rewrite that ? since it got URI encoded by the jQuery.param call a few lines up.

It looks to me like the whole loop "// Build temporary JSONP function" should be run only if you are in jsonp mode. So move the line that sets the dataType to json if jsonp down below that loop, and make the look conditional on jsonp.

And to be absolutely correct, I think you have to look at which parameter has the ? value... otherwise you can run into trouble...

I will try to send a patch later.

Changed December 18, 2007 06:30PM UTC by davidserduke comment:4

There are many different issues and edge cases here. John wrote this comment with the changeset:

Added a fix for bug #2020 - if you want to do data: {callback: "?"}, do jsonp: "callback" instead.

Changed December 19, 2007 02:44AM UTC by john comment:5

resolution: → fixed
status: reopenedclosed

knobunc: We no longer support {callback: "?"} as of jQuery 1.2.2. It's simple too easy to confuse it with a value from a form input element - which is what the data structure is most commonly used for. Instead, please specify the name of the callback using the jsonp: "callback" option.

Changed December 19, 2007 04:15PM UTC by knobunc comment:6

Ah, good. Thanks all. I think that separating the two is absolutely the right thing.