Bug Tracker

Modify

Ticket #4785 (closed bug: duplicate)

Opened 5 years ago

Last modified 3 years ago

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

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

Change History

Changed 5 years ago by DarkRyder

patch against r6388

comment:1 Changed 5 years ago by flesler

  • Cc DarkRyder added
  • Owner set to flesler

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

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

comment:4 Changed 3 years ago by rwaldron

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

comment:5 Changed 3 years ago by rwaldron

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

comment:6 Changed 3 years ago by rwaldron

Duplicate of #2452.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.