Javascript 当我使用getjson调用时,不会加载json数据
我正在用json数据绘制图表 我的控制器返回json数据Javascript 当我使用getjson调用时,不会加载json数据,javascript,json,Javascript,Json,我正在用json数据绘制图表 我的控制器返回json数据 http://localhost:50418/json/index return json 在我看来,我试过这样做,它奏效了,所以问题不在于图表 var data = [], json = [{ "ID": 1, "name": "bob", "score": 212 }, { "ID": 2, "name": "alice", "score": 150 }]; for (var i = 0; i < json.le
http://localhost:50418/json/index return
json
在我看来,我试过这样做,它奏效了,所以问题不在于图表
var data = [],
json = [{ "ID": 1, "name": "bob", "score": 212 }, { "ID": 2, "name": "alice", "score": 150 }];
for (var i = 0; i < json.length; i++) {
data.push({
name: json[i].name,
id: json[i].ID,
y: json[i].score
});
}
如何将数据从控制器获取到变量json???您知道必须将所有代码都放在回调中,对吗
$.getJSON("http://localhost:50418/json/index", function(response){
json = response;
var data = [],
for (var i = 0; i < json.length; i++) {
data.push({
name: json[i].name,
id: json[i].ID,
y: json[i].score
});
}
});
// over here, json is undefined
// stuff here is executed before the getJSON actually gets the response...
打开Chrome Developer Tools,然后在Sources选项卡中找到脚本,并在函数中放置断点。
$.getJSON("http://localhost:50418/json/index", function(response){
json = response;
});
$.getJSON("http://localhost:50418/json/index", function(response){
json = response;
var data = [],
for (var i = 0; i < json.length; i++) {
data.push({
name: json[i].name,
id: json[i].ID,
y: json[i].score
});
}
});
// over here, json is undefined
// stuff here is executed before the getJSON actually gets the response...