2013-04-08 55 views
0

我从一个数组中创建一个列表,然后通过AJAX使用数据参数传递。建立一个列表并发送到一个AJAX方法

当我向控制台写出我的列表时,一切看起来不错,但我的AJAX方法不喜欢它。

for (i = 0; i < carList.length; i++) { 

    var carData = { "data": { "CarId": '"' + CarList[i] + '"', "PassengerCar": "true", "Automatic":"true" } }; 
    console.log(carData); 
} 

$.ajax({ 
dataType: "json", 
data: carData 
.... 

我在做什么不当吗?

感谢

+1

什么是完整的AJAX代码吗?你需要指定url。 – IndoKnight 2013-04-08 20:43:29

+1

你没有构建任何东西,你多次覆盖同一个变量! – adeneo 2013-04-08 20:45:01

+1

@ 999cm999使用提琴手。如果请求失败,它将从服务器返回一个特定的错误。在adeneo之上,你可以在for循环的范围中删除CarData,所以当循环退出时它不存在 – Justin 2013-04-08 20:45:28

回答

2
var carData = {}; 

for (i = 0; i < carList.length; i++) { 

    carData["data_"+i] = { 
          CarId  : CarList[i], 
          PassengerCar: true, 
          Automatic : true 
          }; 

} 

$.ajax({ 
    url  : 'someurl.php', 
    dataType: 'json', //expects returned data from server to be JSON 
    data : carData 
}).done(function(data) { 
    console.log(data); 
});