Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 9 years ago

#2020 closed bug (fixed)

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.diff (1.0 KB) - added by davidserduke 13 years ago.
patch
2020-2.diff (1.3 KB) - added by john 13 years ago.
My stab at it.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 13 years ago by john

Owner: set to john

Changed 13 years ago by davidserduke

Attachment: 2020.diff added

patch

Changed 13 years ago by john

Attachment: 2020-2.diff added

My stab at it.

comment:2 Changed 13 years ago by john

Resolution: fixed
Status: newclosed

Committed in SVN rev [4192].

comment:3 Changed 13 years ago by knobunc

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.

comment:4 Changed 13 years ago by davidserduke

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.

comment:5 Changed 13 years ago by john

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.

comment:6 Changed 13 years ago by knobunc

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

Note: See TracTickets for help on using tickets.