Bug Tracker

Opened 8 years ago

Closed 7 years ago

Last modified 7 years ago

#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
Blocked by: Blocking:

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 (1)

ajax-typefix.diff (701 bytes) - added by DarkRyder 8 years ago.
patch against r6388

Download all attachments as: .zip

Change History (7)

Changed 8 years ago by DarkRyder

Attachment: ajax-typefix.diff added

patch against r6388

comment:1 Changed 8 years ago by flesler

Cc: DarkRyder added
Owner: set to flesler

comment:2 Changed 7 years ago by Rick Waldron

Resolution: invalid
Status: newclosed

$.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 7 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

Version 0, edited 7 years ago by jitter (next)

comment:4 Changed 7 years ago by Rick Waldron

Milestone: 1.4
Resolution: invalid
Status: closedreopened

comment:5 Changed 7 years ago by Rick Waldron

Resolution: duplicate
Status: reopenedclosed

comment:6 Changed 7 years ago by Rick Waldron

Duplicate of #2452.

Note: See TracTickets for help on using tickets.