Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery.get从数据中读取值_Javascript_Jquery - Fatal编程技术网

Javascript jQuery.get从数据中读取值

Javascript jQuery.get从数据中读取值,javascript,jquery,Javascript,Jquery,我似乎无法从jQuery.getJSON访问返回的数据数组中的值,我也无法理解为什么。我在我的应用程序的其他地方也有同样的代码,最大的区别是这个特定实例只返回一行,而不是在其他地方返回多行 当我手动执行脚本时,我可以看到以下JSON输出: [{"total_energy":"34011.920000","earliest_install_date":"2012-01-01"}] 当我执行下面的代码时,数据数组为空/未定义。如果我将“.getJSON”更改为“.get”,我现在可以看到数据中的值

我似乎无法从jQuery.getJSON访问返回的数据数组中的值,我也无法理解为什么。我在我的应用程序的其他地方也有同样的代码,最大的区别是这个特定实例只返回一行,而不是在其他地方返回多行

当我手动执行脚本时,我可以看到以下JSON输出:

[{"total_energy":"34011.920000","earliest_install_date":"2012-01-01"}]
当我执行下面的代码时,数据数组为空/未定义。如果我将“.getJSON”更改为“.get”,我现在可以看到数据中的值,但仍然无法访问它们。我试过通过
数据。总能量
但我得到“未定义”。感谢您的帮助

Javascript代码:

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 was an error retrieving the Environmental Savings data.<br/>If the issue persists please contact SMA for support...<br/>Error: " + sysError);
})
.always(function() {
});
您的JSON是一个数组:

[{"total_energy":"34011.920000","earliest_install_date":"2012-01-01"}]
您需要访问返回的数组的第一个元素,如下所示:

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 was an error retrieving the Environmental Savings data.<br/>If the issue persists please contact SMA for support...<br/>Error: " + sysError);
})
.always(function() {
});
jQuery.getJSON(url、函数(数据){
console.log(“最早日期=”+数据[0]。最早安装日期);
console.log(“总能量=”+数据[0]。总能量);
})
.done(函数(){
})
.fail(函数(jqxhr、textStatus、error){
var sysError=textStatus+,“+错误;
showPopupMsg(errorClass,logoutFlag,“检索环境节约数据时出错。
如果问题仍然存在,请与SMA联系以获取支持…
错误:+sysError); }) .always(函数(){ });
试试这个: jQuery.getJSON(url, {}) .done(function(data) { console.log("Earliest Date= " + data.earliest_install_date); console.log("Total Energy= " + data.total_energy); }) .fail(function(jqxhr, textStatus, error ) { var sysError = textStatus + ", " + error; showPopupMsg(errorClass, logoutFlag, "There was an error retrieving the Environmental Savings data.
If the issue persists please contact SMA for support...
Error: " + sysError); }) .always(function() { }); getJSON(url,{}) .完成(功能(数据){ console.log(“最早日期=“+数据.最早安装日期”); console.log(“总能量=“+数据.总能量”); }) .fail(函数(jqxhr、textStatus、error){ var sysError=textStatus+,“+错误; showPopupMsg(errorClass,logoutFlag,“检索环境节约数据时出错。
如果问题仍然存在,请与SMA联系以获取支持…
错误:+sysError); }) .always(函数(){ });

您忽略了一个事实,即
数据
是一个数组。我们就这样做吧!我还有5分钟等。。。! jQuery.getJSON(url, {}) .done(function(data) { console.log("Earliest Date= " + data.earliest_install_date); console.log("Total Energy= " + data.total_energy); }) .fail(function(jqxhr, textStatus, error ) { var sysError = textStatus + ", " + error; showPopupMsg(errorClass, logoutFlag, "There was an error retrieving the Environmental Savings data.
If the issue persists please contact SMA for support...
Error: " + sysError); }) .always(function() { });