Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#1371 closed enhancement (invalid)

jQuery.param when a value is an Array (improvement)

Reported by: adrien Owned by:
Priority: minor Milestone: 1.2.4
Component: core Version: 1.1.3
Keywords: param Cc:
Blocked by: Blocking:

Description

In the function jQuery.param, when "the value is an array then the key names need to be repeated", it's true, but we need to indicate this was an array then around line 2000:

if ( a[j] && a[j].constructor == Array )
	jQuery.each( a[j], function(){
		s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
	});

should be

if ( a[j] && a[j].constructor == Array )
	jQuery.each( a[j], function(){
		s.push( encodeURIComponent(j) + "[]=" + encodeURIComponent( this ) );
	});

Change History (5)

comment:1 Changed 12 years ago by jakecigar

although a nice idea... it would break every server program!

It's the server's job to realize that it is an array.

comment:2 in reply to:  1 Changed 12 years ago by adrien

Replying to jakecigar:

although a nice idea... it would break every server program!

It's the server's job to realize that it is an array.

I agree with that, but without the hack i submitted it's impossible through GET or POST to know that the values sent are an array! The server can't tell that 'key' is an array in the following GET request: http://example.com/?key=value1&key=value2&key=value3... The request MUST be http://example.com/?key[]=value1&key[]=value2&key[]=value3...

When jQuery serialize a form with $.param, the key names used are the 'input' names, so it respect the form logic, if the names are 'key[]', it will send an array, if the names are 'key', it will send a string overridden by the last value.

But in the case of an array inside an object (the specific case my hack rely to), the key name will be always the same i.e. the object property name, and cannot be 'key[]' because an object property name can't contain brackets!

Now you see the problem? I am clear? (sorry for my english, I'm french)

comment:3 Changed 12 years ago by adrien

oooups, ok I finaly manage to have an object property name with brackets... so my ticket is not valid!

It can be closed

comment:4 Changed 12 years ago by flesler

Resolution: invalid
Status: newclosed

It'd be useful if you could tell how. As far as I know about this (not that much) you can arrange your fields' names as:

<input name="key[0]" />
<input name="key[1]" />
<input name="key[2]" />

or

<input name="key[name]" />
<input name="key[surname]" />
<input name="key[age]" />

This should be recognized (at least by PHP) as an array and will play nicely with $.param.

comment:5 Changed 12 years ago by flesler

Milestone: 1.1.41.2.4
Note: See TracTickets for help on using tickets.