Bug Tracker

Opened 12 years ago

Closed 12 years ago

#8876 closed feature (wontfix)

jQuery.ajax, jQuery.param and form input serialization

Reported by: [email protected] Owned by: [email protected]
Priority: low Milestone: 1.next
Component: ajax Version: 1.5.2
Keywords: Cc:
Blocked by: Blocking:


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.

Change History (4)

comment:1 Changed 12 years ago by timmywil

Component: unfiledajax
Owner: set to [email protected]
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.

comment:2 Changed 12 years ago by Rick Waldron

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 )

comment:3 Changed 12 years ago by [email protected]

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/

comment:4 Changed 12 years ago by timmywil

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.

Note: See TracTickets for help on using tickets.