Skip to main content

Bug Tracker

Side navigation

#11329 closed bug (duplicate)

Opened February 12, 2012 07:40AM UTC

Closed February 12, 2012 01:36PM UTC

Last modified February 12, 2012 01:36PM UTC

.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:
Description

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.

Attachments (0)
Change History (2)

Changed February 12, 2012 01:36PM UTC by sindresorhus comment:1

component: unfiledajax
priority: undecidedlow
resolution: → duplicate
status: newclosed

Changed February 12, 2012 01:36PM UTC by sindresorhus comment:2

Duplicate of #8653.