将数据从 jQuery getJSON 分配给数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3442487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assign data from jQuery getJSON to array
提问by ina
How do you assign the data fetched via getJSON()
to an array, for later use?
你如何将通过获取的数据分配getJSON()
给一个数组,供以后使用?
The getJSON url below retrieves properly formatted JSON with 10 main elements, each having subelements of id, username, haiku (and other). If you're running it, try saving the JSON elements to a local file, so you won't get the same domain error (i.e., JSON will not load if you're fetching from a different domain).
下面的 getJSON url 检索具有 10 个主要元素的格式正确的 JSON,每个元素都有 id、用户名、俳句(和其他)的子元素。如果您正在运行它,请尝试将 JSON 元素保存到本地文件,这样您就不会遇到相同的域错误(即,如果您从不同的域获取数据,则不会加载 JSON)。
What happens is that the alert will fetch a value within the getJSON
callback, but outside, the value is undefined.
发生的情况是警报将在getJSON
回调中获取一个值,但在外部,该值未定义。
$(document).ready(function(){
var haikus=[];
alert("begin loop");
$.getJSON('http://example.com/json.php',function(data){
var i=0;
for(i=0;i<data.length;i++){
haikus[i]=[data[i].id,String(data[i].username),String(data[i].haiku)];
}
alert(haikus[0][1]);
});
})
- Yes, this is related to a previous post. But, I'd initially simplified my problem too much, so the initial solutions provided did not solve it. jQuery - getJSON data to array
- 是的,这与之前的帖子有关。但是,我最初过于简化了我的问题,因此提供的最初解决方案并没有解决它。jQuery - getJSON 数据到数组
回答by user113716
Your issue is that any code outside(and after) the $.getJSON
request has already run by the time the $.getJSON
response is received.
您的问题是,在收到响应时,请求之外(和之后)的任何代码$.getJSON
都已经运行$.getJSON
。
Remember that AJAX calls are asynchronous. This means that the code following the AJAX request does not wait for the response to execute. So it runs long before there's any response.
请记住,AJAX 调用是异步的。这意味着跟随 AJAX 请求的代码不会等待响应执行。所以它在有任何响应之前运行了很长时间。
The key is that any code that relies on the responsefrom an asynchronous AJAX request, must run (or be called from) inside the callback.
关键是任何依赖于异步 AJAX 请求响应的代码都必须在回调中运行(或被调用)。
EDIT:
编辑:
To clarify, in the code example below, please see the code comments. It should help explain the issue.
为了澄清,在下面的代码示例中,请参阅代码注释。它应该有助于解释这个问题。
$(document).ready(function(){
var haikus=[];
alert("begin loop");
$.getJSON('http://haikuennui.com/random.php',function(data){
var i=0;
for(i=0;i<data.length;i++){
haikus[i]=[data[i].id,String(data[i].username),String(data[i].haiku)];
}
// The data in haikus is available here becuase
// this alert() doesn't run until the response is received.
alert(haikus[0][1]);
});
// Here the data in haikus is NOT available because this line
// of code will run ***before*** the response from the AJAX
// request from above is received.
// In other words, this alert() executes **immediately** without
// waiting for the $.getJSON() to receive its response.
alert(haikus[0][1]);
});
回答by kevtrout
I believe if you define a variable by using 'var haikus = something', the variable is of local scope, and if you define the variable by using 'haikus = something', it is of global scope.
我相信如果你使用'var haikus = something'定义一个变量,这个变量是局部范围的,如果你使用'haikus = something'定义一个变量,它是全局范围的。