2010-09-03 23 views
0

我有一個數組:JSON對象應用程序/ x-WWW窗體-urlencoded到MVC控制器

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

我使用這個在一起的其它數據使用jQuery的AJAX方法張貼:

var data = { start: 123403987, list }; 

爲什麼值提交爲:

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

我的期待:

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

回答

2

因爲這是默認的內容類型。你可以指定一個不同的類型。另外,還要確保你有一個有效的JSON對象(這不是你的情況下,使用:代替=的屬性)或服務器端腳本可能會嗆:

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

您使用格式混亂的JSON,它應該是的形式:

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