为什么JSON结果在此JavaScript中"未定义"

Jos*_*ren 2 javascript jquery json

我有一些问题让我的JS正常工作.我试图从下拉列表中选择一个选项,然后调用此函数.正确调用该函数(类型设置为'truck',id设置为5).我想然后使用返回的数据来填充几个字段.我添加到测试中的alert()给了我"undefined".

这是我的JS:

 function getDueDates(type, id) {
     $.getJSON("loadVehicle.php",
     {
         id: id,
         type: type
     },
         function(data) {            
            alert( "TEST: " + data.year);
             $("#inspection_due").val(data.inspection_due);
             $("#short_due").val(data.short_due);
             $("#full_due").val(data.full_due);
         }
 )};
Run Code Online (Sandbox Code Playgroud)

当我手动检查loadVehicle页面(id = 5,type = truck)时,我得到:

[{"truck_id":"5","status":"A","truck_number":"21","year":"1999","make":"Freightliner","model":"Classic","engine":"Detroit","vin_number":"1FUPCSZB2XPA16977","transmission_number":"","tire_size":"","inspection_due":"2009-04-30","short_due":"0000-00-00","full_due":"0000-00-00","comments":"Caf Inc Truck","web_id":"b963940bfd96528f7fd57c08628221f0","last_update":"2009-03-09 16:26:28"}]
Run Code Online (Sandbox Code Playgroud)

但是在页面中警报出现"TEST:undefined"

kar*_*m79 11

您需要data[0].year获得包含单个对象的数组.

  • 该死的速度,karim79.(1) (3认同)