最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Why is the JSON results "undefined" in this JavaScript - Stack Overflow

programmeradmin3浏览0评论

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","ments":"Caf Inc Truck","web_id":"b963940bfd96528f7fd57c08628221f0","last_update":"2009-03-09 16:26:28"}]

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

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","ments":"Caf Inc Truck","web_id":"b963940bfd96528f7fd57c08628221f0","last_update":"2009-03-09 16:26:28"}]

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

Share Improve this question edited Aug 20, 2011 at 4:24 Matt Ball 360k102 gold badges653 silver badges720 bronze badges asked Aug 20, 2011 at 4:19 Josh CurrenJosh Curren 10.2k18 gold badges64 silver badges77 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

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)

  1. 暂无评论