views:

31

answers:

2

I have an array:

var list = JSON.parse('[{"id" = "8", "description" = "test"},{"id" = "10", "description" = "test_2"}]');

I'm using this together other data to post using jQuery's ajax method:

var data = { start: 123403987, list };

Why is the values submitted as:

start=123403987&list[0][id]=8&list[0][description] = "test"...

Where I am expecting:

start=123403987&list[0].id=8&list[0].description = "test"...
A: 

Because that's the default content type. You could specify a different type. Also make sure you have a valid JSON object (which is not your case, use : instead of = for properties) or the server side script might choke:

$.ajax({
    type: 'POST',
    url: '/foo',
    data: '[{ id: "8", description: "test" }, { id: "10", description: "test_2"}]',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(result) {    
        alert('ok');
    }
});
Darin Dimitrov
+1  A: 

You are using poorly formatted JSON, it should be of the form:

var list = JSON.parse('[{"id": "8", "description": "test"},{"id": "10", "description": "test_2"}]');
BeRecursive