Ticket #9658 (closed bug: invalid)
$.ajax should not prepend & to query string data when joining it to a url ending with ?
| Reported by: | abarrett | Owned by: | abarrett |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | ajax | Version: | git |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
An ampersand is unnecessarily prepended to the query string data of a GET request when the URL ends with a question mark.
$.get('foo?', { bar: 42 });
The above example will fetch the URL foo?&bar=42 instead of foo?bar=42.
Concatenation of the URL and query string appears to be performed in ajax.js@L646, with an additional concatenation at L660 when attempting to defeat caching mechanisms.
Those two lines seem to be the only places the rquery regexp is used.
The example may look contrived, but I stumbled across this when using a simple ? as the URL to fetch the current resource with a different query string.
Change History
comment:1 Changed 2 years ago by timmywil
- Owner set to abarrett
- Priority changed from undecided to low
- Status changed from new to pending
- Component changed from unfiled to ajax
comment:2 Changed 2 years ago by abarrett
- Status changed from pending to new
Here is a QUnit test case against edge on jsFiddle: http://jsfiddle.net/alexb/mLS45/
comment:3 follow-up: ↓ 4 Changed 23 months ago by jaubourg
- Status changed from new to closed
- Resolution set to invalid
If you put the question mark in your url, then ajax has to append the parameters the way it does. It cannot guess whether this is intentional on your part or not and takes the safest route, in case this is part of a custom query string scheme that would accept empty string as some kind of id (think myurl?service¶m1=value1&...).
comment:4 in reply to: ↑ 3 Changed 23 months ago by abarrett
Replying to jaubourg:
If you put the question mark in your url, then ajax has to append the parameters the way it does. It cannot guess whether this is intentional on your part or not and takes the safest route, in case this is part of a custom query string scheme that would accept empty string as some kind of id (think myurl?service¶m1=value1&...).
That is a valid point.
How about supporting the special case of a single ? as the URL? As far as I am aware, there is no other way to request the current resource with a different query string.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Thanks for taking the time to contribute to the jQuery project! Please provide a reduced test case on http://jsfiddle.net that reproduces the issue experienced to help us assess your ticket.
Additionally, test against the jQuery (edge) version to ensure the issue still exists.