Ticket #8876 (closed feature: wontfix)
jQuery.ajax, jQuery.param and form input serialization
| Reported by: | christian@… | Owned by: | christian@… |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | ajax | Version: | 1.5.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
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.
Change History
comment:1 Changed 2 years ago by timmywil
- Owner set to christian@…
- Status changed from new to pending
- Component changed from unfiled to ajax
comment:2 Changed 2 years ago by rwaldron
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 2 years ago by christian@…
- Status changed from pending to new
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 2 years ago by timmywil
- Priority changed from undecided to low
- Status changed from new to closed
- Resolution set to wontfix
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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

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.