Ticket #4785 (closed bug: duplicate)
$.get() and $.post() ignore `type` when not passing `data`
| Reported by: | DarkRyder | Owned by: | flesler |
|---|---|---|---|
| Priority: | minor | Milestone: | |
| Component: | ajax | Version: | 1.3.2 |
| Keywords: | Cc: | DarkRyder | |
| Blocking: | Blocked by: |
Description
The $.get() and $.post() functions ignore the type parameter if only the data parameter is omitted. In other words, the following calls all identical in effect:
$.get("foo.php", onSuccess);
$.get("foo.php", onSuccess, "script");
$.get("foo.php", onSuccess, "html");
$.get("foo.php", onSuccess, "not a valid type");
The expected behavior for this particular combination of optional arguments is not explicitly stated in the documentation, but that fact that the first call works (that data is indeed optional) would seem to imply that the others should work as well.
The problem is the conditional block in each function which "shifts" the arguments when data is omitted:
get: function( url, data, callback, type ) {
// shift arguments if data argument was ommited
if ( jQuery.isFunction( data ) ) {
callback = data;
data = null;
}
In the calls above where a type is supplied, it is assigned to the callback parameter, which then gets "overwritten" in this conditional block. This can be easily solved by adding an extra line to the beginning of the block:
get: function( url, data, callback, type ) {
// shift arguments if data argument was omitted
if ( jQuery.isFunction( data ) ) {
type = callback;
callback = data;
data = null;
}
Attached is a patch against the current HEAD (r6388).
Attachments
Change History
Changed 4 years ago by DarkRyder
-
attachment
ajax-typefix.diff
added
comment:2 Changed 3 years ago by rwaldron
- Status changed from new to closed
- Resolution set to invalid
$.get and $.post do not have a type argument per your definition. They do have a dataType argument that defines the format of the data being returned in the server's response.
comment:3 Changed 3 years ago by jitter
I'm pretty sure he meant the dataType parameter. So this ticket isn't invalid but actually a duplicate of #2452 which has already been fixed
comment:4 Changed 3 years ago by rwaldron
- Status changed from closed to reopened
- Resolution invalid deleted
- Milestone 1.4 deleted
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

patch against r6388