Bug Tracker

Opened 10 years ago

Closed 10 years ago

#14120 closed bug (notabug)

$.param does not recursivly serialize return values of functions

Reported by: Sven Schöling Owned by: Sven Schöling
Priority: undecided Milestone: None
Component: unfiled Version: 2.0.3
Keywords: Cc:
Blocked by: Blocking:


Something like

    a: function(){ return [ 1, 2, 3 ] }

does not work, because as soon as buildParams encounters something that is not array nor object, it will call add(prefix, obj), which will break out of the recursion by just encodeURIComponenting the return value.

  • versions affected: found in 1.9.1 debugged in 2.0.3 from git, commit b13d8229.
  • browser: Firefox 23beta
  • OS: Win7
  • console reproduce: decodeURIComponent($.param({a:function(){return[1,2,3]}}))
  • returns: a=1,2,3
  • expected: a[]=1&a[]=2&a[]=3

Change History (3)

comment:1 Changed 10 years ago by dmethvin

Owner: set to Sven Schöling
Status: newpending

Did the documentation lead you to believe that would work? What should we add to the docs to say that it doesn't?

comment:2 Changed 10 years ago by Sven Schöling

Status: pendingnew

The example in the docs of $.param had a nested construct, but did not mention callbacks.

I had the general assumption that anonymous callbacks work (to my knowledge) nearly everywhere in jquery as replacement for data. And they do work here, they just behave differently.

Serializing flat form data never had to deal with recursion and I don't know the intend behind enabling recursive parsing in the first place. But I'd argue that if callbacks are allowed in there their behaviour should be consistent with static data.

If this is not wanted for backwards compatibility, it would be nice to document this.

comment:3 Changed 10 years ago by dmethvin

Resolution: notabug
Status: newclosed

Since $.param is intended to serialize data that came out of form elements, there isn't a situation where a function should be encountered. As the docs say,

This function is used internally to convert form element values into a serialized string representation (See .serialize() for more information)

The current behavior is as good as any for unexpected inputs. It doesn't seem worth documenting because nobody has ever asked about this before so it's not a common misconception.

Note: See TracTickets for help on using tickets.