jQuery: The Write Less, Do More JavaScript Library

Ticket #2020 (closed bug: fixed)

Opened 5 months ago

Last modified 5 months ago

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

Reported by: knobunc Assigned to: john
Type: bug Priority: major
Milestone: 1.2.2 Component: ajax
Version: 1.2.1 Keywords: ajax jsonp ? json
Cc: Needs: Review

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

2020.diff (1.0 kB) - added by davidserduke 5 months ago.
patch
2020-2.diff (1.3 kB) - added by john 5 months ago.
My stab at it.

Change History

Changed 5 months ago by john

  • owner set to john

Changed 5 months ago by davidserduke

patch

Changed 5 months ago by john

My stab at it.

Changed 5 months ago by john

  • status changed from new to closed
  • resolution set to fixed

Committed in SVN rev [4192].

Changed 5 months ago by knobunc

  • status changed from closed to reopened
  • resolution 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.

Changed 5 months 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.

Changed 5 months 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.

Changed 5 months 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.