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?

1
  • 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); Commented Jul 14, 2015 at 18:57

3 Answers 3

1

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

var obj = {order: Array[1], type: Array[1]}
Sign up to request clarification or add additional context in comments.

2 Comments

How would I declare the object and then subsequently add new items to it after some if statements?
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".
1

I think you need to declare filters as an object:

filters = {};
this.filters.order = order;

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.