Bug Tracker

Modify

Ticket #1371 (closed enhancement: invalid)

Opened 6 years ago

Last modified 5 years ago

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:
Blocking: Blocked by:

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

comment:1 follow-up: ↓ 2 Changed 6 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 6 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 6 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 5 years 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.

comment:5 Changed 5 years ago by flesler

  • Milestone changed from 1.1.4 to 1.2.4

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.