开发者

json object submitted as application/x-www-form-urlencoded to mvc controller

开发者 https://www.devze.com 2023-01-14 13:34 出处:网络
I have an array: var list = JSON.parse(\'[{\"id\" = \"8\", \"description\" = \"test\"},{\"id\" = \"10\", \"descriptio开发者_StackOverflown\" = \"test_2\"}]\');

I have an array:

var list = JSON.parse('[{"id" = "8", "description" = "test"},{"id" = "10", "descriptio开发者_StackOverflown" = "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"...


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');
    }
});


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"}]');
0

精彩评论

暂无评论...
验证码 换一张
取 消