Bug Tracker

Modify

Ticket #11329 (closed bug: duplicate)

Opened 15 months ago

Last modified 15 months ago

.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:
Blocking: Blocked by:

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.

Change History

comment:1 Changed 15 months ago by sindresorhus

  • Priority changed from undecided to low
  • Resolution set to duplicate
  • Status changed from new to closed
  • Component changed from unfiled to ajax

comment:2 Changed 15 months ago by sindresorhus

Duplicate of #8653.

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.