Bug Tracker

Ticket #1371 (closed enhancement: invalid)

Opened 2 years ago

Last modified 2 months ago

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

Reported by: adrien Assigned to: anonymous
Type: enhancement Priority: minor
Milestone: 1.2.4 Component: core
Version: 1.1.3 Keywords: param
Cc: Needs: Review

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 ) );
	});

Attachments

Change History

follow-up: ↓ 2   Changed 2 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.

in reply to: ↑ 1   Changed 2 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)

  Changed 2 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

  Changed 8 months ago by flesler

  • status changed from new to closed
  • resolution set to invalid

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.

  Changed 8 months ago by flesler

  • milestone changed from 1.1.4 to 1.2.4
Note: See TracTickets for help on using tickets.