Bug Tracker

Opened 7 years ago

Closed 5 years ago

Last modified 5 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).

Change History (6)

comment:1 Changed 7 years ago by flesler

  • Cc DarkRyder added
  • Owner set to flesler

comment:2 Changed 6 years ago by rwaldron

  • Resolution set to invalid
  • Status changed from new to closed

$.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 5 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 5 years ago by jitter (next)

comment:4 Changed 5 years ago by rwaldron

  • Milestone 1.4 deleted
  • Resolution invalid deleted
  • Status changed from closed to reopened

comment:5 Changed 5 years ago by rwaldron

  • Resolution set to duplicate
  • Status changed from reopened to closed

comment:6 Changed 5 years ago by rwaldron

Duplicate of #2452.

Note: See TracTickets for help on using tickets.