首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Jquery将ajax响应保存到数组中

用Jquery将ajax响应保存到数组中
EN

Stack Overflow用户
提问于 2013-09-17 13:43:15
回答 4查看 10K关注 0票数 1

我得到了一个Jquery调用,它从我得到的php文件中获得一个json响应,json响应很棒,我可以正确地记录响应,但是我似乎无法保存ajax结果之外的信息,它不会更新数组。代码看起来是这样的,我在下面发布了结果。

代码语言:javascript
复制
window.array={name: 'john',color:'red'};    

 console.log(array['name']);
 console.log(array['color']);

$.ajax({
    url : 'getJsons.php',
    type : 'POST',
    data : data,
    dataType : 'json',
    success : function (data) {

      array['name'] = data['name'];
      array['color'] = data['color'];
      console.log(array['name']);
      console.log(array['color']);
     }        
});

 console.log(array['name']);
 console.log(array['color']);

这将导致以下控制台:

代码语言:javascript
复制
 john
 red

 john
 red

 marry
 blue

因此,我第一次正确地获得了控制台,但是它似乎在ajax调用之前加载了脚本,为什么呢?因为这使得我不可能在其余的代码中使用ajax结果,因为它是在脚本加载之后获取的。是否有办法使ajax在其余部分之前运行?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-09-17 13:50:49

由于您不可能知道来自服务器的ajax响应何时到达,因此AJAX默认是异步的。这意味着$.ajax被触发,然后javascript引擎继续执行剩下的代码(在您的示例中,两个console.log在ajax调用之外)。在将来的某个地方,ajax调用可能(也可能不会)从服务器获得响应(并通过更改状态通知这一点)。此时,javascript引擎将处理所谓的“回调”函数代码,这些代码将在ajax调用完成时执行。将回调函数定义为ajax方法的success参数。

这就是为什么执行代码的正确方法是运行一切,这取决于回调函数的结果。将所有内容直接放在其中,或者声明一个独立的函数,然后在成功函数中调用该函数:

代码语言:javascript
复制
$.ajax({
    /*...*/,
    success : function (data) {

      array['name'] = data['name'];
      array['color'] = data['color'];

      /* either put all your dependent code here */

      /* or just call a callback function: */
      callback(data);

     }        
});

/* code to be executed after the ajax call */
function callback(data){

   // work with the response here
   console.log(data);

}

前面的坏主意:

或者,您可以告诉调用是同步的(这是不好的,因为您的浏览器基本上是冻结的,而它等待来自服务器的答案),并保持您的代码原样。

代码语言:javascript
复制
$.ajax({
      /*...*/,
      async : false,     
});
// risk some waiting time (possibly the full duration of a timeout!!)
// the browser is locked during this time

// continue normally
console.log(array['name']);
console.log(array['color']);
票数 4
EN

Stack Overflow用户

发布于 2013-09-17 13:46:02

正在发生的是在ajax调用完成之前运行查询之后的代码。您的数组已经填充,不要担心,但是由于AJAX异步运行,它只会在ajax调用之后分配值。

例如,如果在ajax调用之后设置了10秒超时(取决于AJAX调用所用的时间),然后调用数组值,则会发现它们已填充(前提是AJAX已正确运行并正确地遍历回调函数)。

因此,在您的代码中,一步一步地看到了所发生的事情。

You display the values from the array which shows john red

You make the AJAX call, when this call has completed it will perform your success: function

The ajax call takes (lets say) 1 second to complete

Whilst your page is waiting for the call to complete and update the values it moves onto the next bit of code which displays the array, as the array hasn't been updated yet as success hasn't been called it still displays john red

1 seconds later the success function is called, assigns the new names to the array and prints them out.

票数 1
EN

Stack Overflow用户

发布于 2013-09-17 13:46:09

ajax调用是异步的,这意味着无论ajax何时执行和返回,代码的其余部分都将被执行。如果您的代码依赖于结果,那么将其封装到一个函数中,并从ajax的回调(成功函数)中调用它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18851619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档