Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#13611 closed bug (duplicate)

$.param should treat empty arrays/objets like empty strings

Reported by: daniellibanori@… Owned by: daniellibanori@…
Priority: low Milestone: None
Component: ajax Version: 1.9.1
Keywords: Cc:
Blocked by: Blocking:

Description

It can't send empty array via Ajax:

$.post('/foo', { empty: [] });

Related: http://bugs.jquery.com/ticket/6481

Change History (8)

comment:1 Changed 7 years ago by timmywil

Component: unfiledajax
Owner: set to daniellibanori@…
Priority: undecidedlow
Status: newpending

From the discussion in the ticket you've linked, it sounds like it shouldn't. I think we'd be happy to hear your argument though.

comment:2 Changed 7 years ago by Daniel Libanori <daniellibanori@…>

The main question here is: how to send an empty array to server?

Actually, this issue is related with $.param.

comment:3 Changed 7 years ago by dmethvin

Resolution: duplicate
Status: pendingclosed

Duplicate of #6481.
You're right, that is the question. How would you send an empty array using a form post without JavaScript? You wouldn't. The application/x-www-form-urlencoded MIME type doesn't support it. There is no governing standard or de-facto convention for how to send an array via a form.

The client and server can create any convention they'd like regarding what string might represent an empty array, but that's not something jQuery can help you with. We only handle the client end.

To send complex JavaScript-like data structures that don't resemble user form data entries, use JSON.

comment:4 Changed 7 years ago by Daniel Libanori <daniellibanori@…>

So you should remove $.param ability to send non-empty arrays.

Why can you send not empty array but you can't send empty array. This seems arbitrary.

comment:5 Changed 7 years ago by dmethvin

It *is* arbitrary to some extent since there is no standard. We tried it both ways and this was the best compromise that broke the fewest things. Deal with it on your server, or convert the empty array to a string if you have no control over the server. You could also start lobbying to create a standard here, but I would guess that nobody would care enough to do anything about it since there are better solutions like JSON. Just don't blame jQuery, we can't read minds.

comment:6 Changed 7 years ago by Daniel Libanori <daniellibanori@…>

I do know you can't read minds, so I wrote a bug report instead of just blaming jQuery. :)

I do think people care about it, as I am not the first one complaining about empty array serialization.

jQuery already serializes arrays, just not the empty ones. The current array serialization format supports an empty array, jQuery just doesn't serialize it. Why?

comment:7 Changed 7 years ago by dmethvin

Go back and read the old ticket. Summarize here.

comment:8 Changed 7 years ago by Daniel Libanori <daniellibanori@…>

I got it. The main problem is { a: [''] } would look like { a: [] }.

Thank for your attention.

Last edited 7 years ago by dmethvin (previous) (diff)
Note: See TracTickets for help on using tickets.