Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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$.getJSON错误_Javascript_Jquery_Json - Fatal编程技术网

Javascript jquery$.getJSON错误

Javascript jquery$.getJSON错误,javascript,jquery,json,Javascript,Jquery,Json,使用floatcharts jquery工具,数据var值如下 var d2 = [[0,0],[1,0],[2,0],[3,0],[4,"1"],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0]] 当我使用$getJSON将其更改为从请求中获取时 var d2 = var d2 = $.getJSON( "UpdateCharts", function() { ...... 响应返回为 [[0,0],[1,0],[2,0],[3,0],[4,"1"]

使用floatcharts jquery工具,数据var值如下

var d2 = [[0,0],[1,0],[2,0],[3,0],[4,"1"],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0]]
当我使用$getJSON将其更改为从请求中获取时

 var d2 = var d2 = $.getJSON( "UpdateCharts", function() { ......
响应返回为

[[0,0],[1,0],[2,0],[3,0],[4,"1"],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0]]
但我得到一个错误,d2值是0,和第一个值不一样

Uncaught TypeError: Cannot read property '0' of undefined
试试这个(这个很有效):


jQuery是异步的。它不会
返回
响应,而是调用您必须提供的
成功
回调,并将响应作为参数传递。相关:@JonathanLonowski,如果他需要与json文件相同的结构,那么可以has@MostafaKasem你到底试了什么?显示JSFIDLE请使用$.ajax而不是$.getJSON进行修复,您的重播是正确的(Y)不能像那样使用$.getJSON。
var d2;
$.getJSON( "path/to/json/file", function(data) { 
   // parse data to the d2
   // d2 = data; // <-- use this if you want same array/object structure like json file contents has
   console.log(d2); // <-- this will output correct d2 contents 
}
var d2 = $.getJSON( "path/to/json/file", function(data) { 
   // parse data to the d2
}
console.log(d2); // <-- this will show error/wrong contents, because it is shown before json data was parsed
1. // this part is executing first, order: 1
2. var xxx = $.getJSON( "xxx", function(xxx) { // you just specified what to do when data will be parsed there
3.    // this part will be called after all contents will be loaded, order: 3
4. }
5. // this part is executing right after 1st lane, order: 2