#2452 closed bug (fixed)
$.post and $.get are probably not handling the abcense of data well
Reported by: | adsmart | Owned by: | flesler |
---|---|---|---|
Priority: | major | Milestone: | 1.4 |
Component: | ajax | Version: | 1.2.3 |
Keywords: | Cc: | adsmart, nathanhammond | |
Blocked by: | Blocking: |
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 (1)
Change History (9)
comment:1 Changed 15 years ago by
Component: | core → ajax |
---|
comment:2 Changed 15 years ago by
comment:4 Changed 15 years ago by
Cc: | adsmart nathanhammond added |
---|---|
Milestone: | 1.2.4 → 1.3 |
need: | Review → Commit |
Owner: | set to flesler |
Priority: | critical → major |
Status: | new → assigned |
Ok, thanks for the patch. I'll review this asap.
comment:5 Changed 14 years ago by
Ticket #3775 has gotten some attention on this issue, too. I think we can close it as a dupe.
Ariel, can we drop in Nathan's non-intrusive patch?
comment:6 Changed 14 years ago by
A patch is included with dupe #4384 with a test case and a fix for the comment typo.
comment:7 Changed 14 years ago by
Milestone: | 1.3 → 1.3.3 |
---|---|
Resolution: | → fixed |
Status: | assigned → closed |
Fixed at [6564]
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:
Then you can test each parameter individually, and if set to 'undefined' you can ignore it or set a default.
i.e.: