Skip to main content

Bug Tracker

Side navigation

#14120 closed bug (notabug)

Opened July 10, 2013 06:27PM UTC

Closed July 26, 2013 02:33PM UTC

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

Something like

  $.param({
    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
encodeURIComponent
ing 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
Attachments (0)
Change History (3)

Changed July 10, 2013 06:29PM UTC by dmethvin comment:1

owner: → 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?

Changed July 11, 2013 08:47AM UTC by Sven Schöling comment:2

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.

Changed July 26, 2013 02:33PM UTC by dmethvin comment:3

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.