javascript - jQuery.get read values from data -
i cannot seem access values in returned info array jquery.getjson , cannot understand why. have same code working elsewhere in app, biggest difference beingness particular instance returns 1 row only, opposed multiple in other places.
when manually execute script can see json output:
[{"total_energy":"34011.920000","earliest_install_date":"2012-01-01"}] when execute code below, info array empty / undefined. if alter ".getjson" ".get" can see values in info still cannot access them. have tried via data.total_energy "undefined". help appreciated.
the javascript code:
jquery.getjson(url, function(data) { console.log("earliest date= " + data.earliest_install_date); console.log("total energy= " + data.total_energy); }) .done(function() { }) .fail(function(jqxhr, textstatus, error ) { var syserror = textstatus + ", " + error; showpopupmsg(errorclass, logoutflag, "there error retrieving environmental savings data.<br/>if issue persists please contact sma support...<br/>error: " + syserror); }) .always(function() { }); the result in console is:
earliest date= undefined total energy= undefined
your json array:
[{"total_energy":"34011.920000","earliest_install_date":"2012-01-01"}] you need access first element of array returned so:
jquery.getjson(url, function(data) { console.log("earliest date= " + data[0].earliest_install_date); console.log("total energy= " + data[0].total_energy); }) .done(function() { }) .fail(function(jqxhr, textstatus, error ) { var syserror = textstatus + ", " + error; showpopupmsg(errorclass, logoutflag, "there error retrieving environmental savings data.<br/>if issue persists please contact sma support...<br/>error: " + syserror); }) .always(function() { }); javascript jquery
No comments:
Post a Comment