Ticket #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: | |
| Blocking: | Blocked by: |
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)
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:
But not in releases preceeding 1.2.
Attachments
Change History
comment:2 Changed 4 years ago by john
- Status changed from new to closed
- Resolution set to fixed
Committed in SVN rev [4192].
comment:3 Changed 4 years ago by knobunc
- Status changed from closed to reopened
- Resolution fixed deleted
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 4 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 4 years ago by john
- Status changed from reopened to closed
- Resolution set to fixed
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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

