将jQuery getJSON中的数据分配给数组 [英] Assign data from jQuery getJSON to array

查看:101
本文介绍了将jQuery getJSON中的数据分配给数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将通过getJSON()获取的数据分配给数组以供以后使用?

How do you assign the data fetched via getJSON() to an array, for later use?

下面的getJSON url检索具有10个主要元素的格式正确的JSON,每个主要元素具有id,username,haiku(及其他)的子元素.如果您正在运行它,请尝试将JSON元素保存到本地文件中,这样就不会出现相同的域错误(即,如果您是从其他域获取的,则将不会加载JSON).

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回调中获取一个值,但是在外部,该值是未定义的.

What happens is that the alert will fetch a value within the getJSON callback, but outside, the value is undefined.



$(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]);
    });
})

  • 是的,这与先前的帖子有关.但是,我最初将我的问题简化得太多,因此提供的初始解决方案无法解决问题. jQuery-将getJSON数据存储到数组
    • 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
    • 推荐答案

      您的问题是,在$.getJSON请求之前,任何外部代码(以及之后)都已经运行了$.getJSON请求.收到响应.

      Your issue is that any code outside (and after) the $.getJSON request has already run by the time the $.getJSON response is received.

      请记住,AJAX调用是异步的.这意味着AJAX请求 之后的代码不会等待响应执行 .因此,它运行很长时间才得到任何回应.

      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请求的响应的代码 ,都必须在回调内部运行(或从中调用).

      The key is that any code that relies on the response from an asynchronous AJAX request, must run (or be called from) inside the callback.

      为澄清起见,在下面的代码示例中,请参阅代码注释.它应该可以帮助解释这个问题.

      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]);
      
      });
      

      这篇关于将jQuery getJSON中的数据分配给数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆