Bug Tracker

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#11329 closed bug (duplicate)

.param() Should Return Empty Values for Null and Undefined

Reported by: wardrop Owned by:
Priority: low Milestone: None
Component: ajax Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:


I'm going to call this a bug as the behaviour is generally undesirable and the assumption it encourages developers to make can be dangerous. Otherwise, I guess it's an enhancement on existing known behaviour.

Currently, anything given to .param() that has a null or undefined value, is returned in the serialized string as a literal "null" or "undefined" string. For example:

$.param({name: undefined, age: null}) // Returns: "name=undefined&age=null"

The problem with this is two-fold. First, your server needs to be able to be aware of the fact that empty values could be an empty string "", or a literal "null" or "undefined". This introduces the second problem where if you make your server-side code aware of this, it will always convert "null" and "undefined" string values to null, meaning that a user cannot enter "null" or "undefined" as a value.

It's dangerous therefore for the server to presume that all "null" and "undefined" string represent those types. I propose that the behaviour of .param() be changed to return empty values for null and undefined. E.g.

 $.param({name: undefined, age: null}) // Should return: "name=&age="

The fix would be simple enough. Insert before line 7602 of the development version of 1.7.1, a simple pre-process condition, e.g.

value = (value == null) ? '' : value

In the mean time, I've overridden the built-in encodeURIComponent() function to return an empty string for null or undefined values, as there's otherwise no easy way to override this behaviour of jQuery at runtime.

Change History (2)

comment:1 Changed 11 years ago by sindresorhus

Component: unfiledajax
Priority: undecidedlow
Resolution: duplicate
Status: newclosed

comment:2 Changed 11 years ago by sindresorhus

Duplicate of #8653.

Note: See TracTickets for help on using tickets.