1

I need to send an array via ajax, but for some reason it will not send, I suspect it is due to serialisation.

So how would I create the array so it is ajax compatible?

I need the array in this format, a key value pair where the value is another array.

[order: Array[1], type: Array[1]]

I declare my array like so:

filters = [];

If the inputs are checked I add them to the array.

var order = $('input[name="order[]"]:checked').map(function(){return this.value;}).get();
if(!$.isEmptyObject(order))this.filters['order'] = order;
....

With the idea, if no inputs are selected, the array is blank and therefore is not sent by ajax.

How can I maintain the structure of the array but make it compatible with ajax?

panthro
  • 22,779
  • 66
  • 183
  • 324
  • Ajax doesn't technically have any formating requirements. It's not a stand alone technology like javascript it's a methodology. The data reciever will usually determine the format you send it in. For php or other javascript applets you usually use json encoding. But you can also use xml or your own custom setup. try JSON.stringify(values); – danielson317 Jul 14 '15 at 18:57

3 Answers3

1

Arrays don't use key-value pairs. Instead, use an object.

var obj = {order: Array[1], type: Array[1]}
whatoncewaslost
  • 2,216
  • 2
  • 17
  • 25
  • How would I declare the object and then subsequently add new items to it after some if statements? – panthro Jul 14 '15 at 18:54
  • Easy! Just declare `var obj = {}` and then when you want to add a property, use dot notation, i.e. `obj.propertyToAdd = "This is a property"`. – whatoncewaslost Jul 14 '15 at 18:56
1

I think you need to declare filters as an object:

filters = {};
this.filters.order = order;
flyingL123
  • 7,686
  • 11
  • 66
  • 135
0

If you're using jQuery, see my answer here. I had a similar issue. When you're passing an array or objects, you have to use JSON.stringify() and set the content type.

Community
  • 1
  • 1
Halcyon
  • 14,631
  • 17
  • 68
  • 99