10

The following code works for me using jQuery 1.2.6 but causes a broker error in 1.4.

var items = new Array();
items[0] = "Item 1";
items[1] = "Item 2";
items[2] = "Item 3";

var dataToSend = {'_service' : myService, '_program' : myProgram, 'selections' : items} ;

$.ajax({    
 type: "post",
 url: myURL,
 dataType: "text",
 data: dataToSend,
 success: function(request) {$('#results').html(request); } // End success
}); // End ajax method

The broker error I get indicates that what is being passed in selections is 'selections[]'

ERROR:(Invalid character "[" in field name "selections[]". This character is not allowed in field names.)

Was there a change in how jQuery handles arrays in an ajax call? or was this an incorrect way to pass an array?

Any help would be appreciated.

EDIT: The answer from @jvenema solved my problem. With the "traditional" setting you can cause jQuery to handle the parameters like the previous version. Here are some additional links which talk about the change jQuery.ajax(), jQuery.param() and a blog post jQuery 1.4 $.param demystified.

Either a general statement of

jQuery.ajaxSettings.traditional = true;

or as an additional option in the ajax call

$.ajax({    
 traditional: true,
 type: "post",
 url: myURL,
 dataType: "text",
 data: dataToSend,
 success: function(request) {
   $('#results').html(request);
 }  // End success
}); // End ajax method
Jay Corbett
  • 28,091
  • 21
  • 57
  • 74

2 Answers2

21

jQuery 1.4 was updated to use the PHP syntax for sending arrays. You can switch it into the old style by using:

jQuery.ajaxSetting.traditional = true;

See here for details.

Jerod Venema
  • 44,124
  • 5
  • 66
  • 109
0

You can specify the array as Json array.

'selections' : {items : ['Item 1', 'Item 2', 'Item 3']}

I think that this will work too.

'selections' : {items : items}

Take a look here for addition resource.

Community
  • 1
  • 1
Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96