开发者

Why is the JSON results "undefined" in this JavaScript

I am having some issues getting my JS to work right. I am trying to select an option from a dropdown which then calls this function. The function is being called properly (type is set to 'truck' and id is set to 5). I want to then use the data returned to populate several fields. The alert() I added to test gives me "undefined".

Here is my 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);
         }
 开发者_如何转开发)};

When I check the loadVehicle page manually (with id=5, type=truck) I get:

[{"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"}]

But in the page the alert comes up with "TEST: undefined"


You need data[0].year since you are getting an array containing a single object.


Try data[0].year.

It looks like your loadVehicle.php is returning an array of objects not just one object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜