Bug Tracker

Ticket #2452 (assigned bug)

Opened 6 months ago

Last modified 2 weeks ago

$.post and $.get are probably not handling the abcense of data well

Reported by: adsmart Assigned to: flesler (accepted)
Type: bug Priority: major
Milestone: 1.3 Component: ajax
Version: 1.2.3 Keywords:
Cc: adsmart, nathanhammond Needs: Commit

Description

I was looking for the post and get code today (I'm planning on replicating them for the DELETE and PUT verbs... those should be added) and I noticed a funny problem with the following (GET has the same problem):

	post: function( url, data, callback, type ) {
		if ( jQuery.isFunction( data ) ) {
			callback = data;
			data = {};
		}
		return jQuery.ajax({
			type: "POST",
			url: url,
			data: data,
			success: callback,
			dataType: type
		});
	},

You make a check to see if the second argument is a function, indicating that no data is being sent. That's fine but it ignores the fact that the type might also be passed. As a result, the following would have unintended effects:

$.post("/sample/test", do_stuff_function, "text/html");

It also doesn't take into account

$.post("/sample/test", "text/html");

Which, while probably silly in most cases, is legitimate within the published docs for the functions.

I suspect the following would be a better solution (depending on how you feel about using arguments.length, you could use other methods to identify things):

if (arguments.length == 2 ) {
    type = data;
    callback = function () {};
    data = {};
} else if (arguments.length == 3) {
    type = callback;
    callback = data;
    data = {};
};

Attachments

argshift.diff (0.6 kB) - added by nathanhammond 2 weeks ago.
Makes sure the argument shift is complete.

Change History

Changed 4 months ago by flesler

  • owner deleted
  • component changed from core to ajax

Changed 3 months ago by ygirouard

A fullproof solution would probably be to force users to pass an array of options (array object or json), much like you do for $.ajax(); That way it avoids having to deal with omited arguments since you reference each as an object property.

For example:

$.get({url: "/sample/test", callback: do_stuff_function, type: "html"});

Then you can test each parameter individually, and if set to 'undefined' you can ignore it or set a default.

i.e.:

  post: function(params) {
    if (params.data == undefined || params.data == ""){params.data = {};}
    // Do other testing here if needed...
    return jQuery.ajax({
      type: "POST",
      url: params.url,
      data: params.data,
      success: params.callback,
      dataType: params.type
    });
  },

Changed 2 weeks ago by nathanhammond

Makes sure the argument shift is complete.

Changed 2 weeks ago by nathanhammond

I've attached a patch that can close this issue.

Changed 2 weeks ago by flesler

  • status changed from new to assigned
  • cc set to adsmart, nathanhammond
  • priority changed from critical to major
  • owner set to flesler
  • milestone changed from 1.2.4 to 1.3
  • need changed from Review to Commit

Ok, thanks for the patch. I'll review this asap.

Note: See TracTickets for help on using tickets.