Skip to main content

Bug Tracker

Side navigation

#8876 closed feature (wontfix)

Opened April 14, 2011 05:33PM UTC

Closed April 14, 2011 07:54PM UTC

jQuery.ajax, jQuery.param and form input serialization

Reported by: christian@christianomania.com Owned by: christian@christianomania.com
Priority: low Milestone: 1.next
Component: ajax Version: 1.5.2
Keywords: Cc:
Blocked by: Blocking:
Description

It seems that it's a mistake to rely on the default data serialization (jQuery.param) used by jQuery.ajax. Specifically when data is an array of form elements.

While the API docs point out that jQuery.param should only be used for objects, the comments in code indicate otherwise https://github.com/jquery/jquery/blob/1.5.2/src/ajax.js#L740:

"// Serialize an array of form elements..."

For starters I'd advocate to removing the comment from the code. I also think it would be nice to perform some detection on data such that jQuery.serializeArray gets called if necessary.

While it can be said rtfm, "don't pass form inputs to jQuery.ajax (and by extension jQuery.param)",because jQuery.param does successfully serialize *some* form elements it's not immediately clear that this a bad idea.

Attachments (0)
Change History (4)

Changed April 14, 2011 05:41PM UTC by timmywil comment:1

component: unfiledajax
owner: → christian@christianomania.com
status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a reduced test case on http://jsFiddle.net that reproduces the issue experienced to help us assess your ticket. Perhaps you could provide a certain use case where jQuery.param or jQuery.serializeArray fails where it shouldn't.

Additionally, test against the jQuery (edge) version to ensure the issue still exists.

Changed April 14, 2011 06:09PM UTC by rwaldron comment:2

I'm not sure I understand what the request is... it says "feature" but reads like a "bug". Any clarification would also be helpful (in addition to timmywil's requests )

Changed April 14, 2011 07:31PM UTC by christian@christianomania.com comment:3

status: pendingnew

The reason I put this down as a feature vs a bug is that it's possible to say that you're not supposed to pass an array of form elements as data into jQuery.ajax. At the same time it seems logical/plausible to do so and the data is happily serialized (albeit incorrectly).

I've set up an example here: http://jsfiddle.net/NfnUX/36/

Changed April 14, 2011 07:54PM UTC by timmywil comment:4

priority: undecidedlow
resolution: → wontfix
status: newclosed

That is the correct behavior. As you noted, doing makeArray instead of serializeArray passes an unfiltered array of elements to jQuery.param. Skipping serializeArray and only doing jQuery.param is the only way to include elements without being filtered first, which is why you are seeing the names and values for unchecked and disabled checkboxes. Putting that code in param would defeat the purpose of having serializeArray and would break param, which only serializes the data you give it and is useful because of its simplicity.