<legend id='zlD9I'><style id='zlD9I'><dir id='zlD9I'><q id='zlD9I'></q></dir></style></legend>

  • <i id='zlD9I'><tr id='zlD9I'><dt id='zlD9I'><q id='zlD9I'><span id='zlD9I'><b id='zlD9I'><form id='zlD9I'><ins id='zlD9I'></ins><ul id='zlD9I'></ul><sub id='zlD9I'></sub></form><legend id='zlD9I'></legend><bdo id='zlD9I'><pre id='zlD9I'><center id='zlD9I'></center></pre></bdo></b><th id='zlD9I'></th></span></q></dt></tr></i><div id='zlD9I'><tfoot id='zlD9I'></tfoot><dl id='zlD9I'><fieldset id='zlD9I'></fieldset></dl></div>
    <tfoot id='zlD9I'></tfoot>

    <small id='zlD9I'></small><noframes id='zlD9I'>

      • <bdo id='zlD9I'></bdo><ul id='zlD9I'></ul>

        循环内的 jQuery.ajax()

        jQuery.ajax() inside a loop(循环内的 jQuery.ajax())

            <legend id='dKILd'><style id='dKILd'><dir id='dKILd'><q id='dKILd'></q></dir></style></legend><tfoot id='dKILd'></tfoot>
            • <bdo id='dKILd'></bdo><ul id='dKILd'></ul>

                <small id='dKILd'></small><noframes id='dKILd'>

                  <tbody id='dKILd'></tbody>

                • <i id='dKILd'><tr id='dKILd'><dt id='dKILd'><q id='dKILd'><span id='dKILd'><b id='dKILd'><form id='dKILd'><ins id='dKILd'></ins><ul id='dKILd'></ul><sub id='dKILd'></sub></form><legend id='dKILd'></legend><bdo id='dKILd'><pre id='dKILd'><center id='dKILd'></center></pre></bdo></b><th id='dKILd'></th></span></q></dt></tr></i><div id='dKILd'><tfoot id='dKILd'></tfoot><dl id='dKILd'><fieldset id='dKILd'></fieldset></dl></div>
                  本文介绍了循环内的 jQuery.ajax()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  如果我在循环中调用 jQuery.ajax(),是否会导致当前迭代中的调用覆盖上次调用或为新请求分配新的 XHR 对象?

                  If I call jQuery.ajax() inside a loop, would it cause the call in current iteration overwrite the last call or a new XHR object is assigned for the new request?

                  我有一个循环来执行此操作,而从控制台日志中我可以看到请求已完成 200 ok 但只有循环中最后一个请求的结果数据由请求 success 存储回调 .

                  I have a loop that do this, while from console log I can see requests done 200 ok but just the result data of the last request in the loop is stored by the request success callback as supposed .

                  代码:

                  var Ajax = {
                      pages: {},
                  
                      current_request: null,
                  
                      prefetch: function () {
                          currentPath = location.pathname.substr(1);
                  
                          if(this.pages[currentPath])
                          {
                              var current = this.pages[currentPath];
                              delete this.pages[currentPath];
                  
                              current['name']=currentPath;
                              current['title']=$("title").text().replace(' - '.SITE_NAME, '');
                              current['meta_description']=$("meta[name=description]").attr('content');
                              current['meta_keywords']=$("meta[name=keywords]").attr('content');          
                          }
                  
                          var _Ajax = this;
                          //the loop in question *****
                          for(var key in this.pages)
                          {
                              $.ajax({
                                  method: 'get',
                                  url:'http://'+location.hostname+'/'+key,
                                  success: function(data) {
                                      _Ajax.pages[key] = data;    
                                  }
                              }); 
                  
                                      console.debug(this.pages);
                          }
                  
                          if(current)
                          {
                              this.pages[currentPath] = current;
                          }       
                  
                      } 
                  };//Ajax Obj
                  for(var i in pages)
                  {
                      Ajax.pages[pages[i]]={};
                  }
                  
                  $(function() {
                      Ajax.prefetch();
                  });//doc ready
                  

                  推荐答案

                  你需要一个 key 的闭包:

                  You'll need a closure for key:

                  for(var k in this.pages){
                      (function(key){
                              $.ajax({
                                  method: 'get',
                                  url:'http://'+location.hostname+'/'+key,
                                  success: function(data) {
                                      _Ajax.pages[key] = data;    
                                  }
                              }); 
                  
                              console.debug(this.pages);
                      })(k);
                  }
                  

                  这样您就可以确保在每个 ajax 成功回调中该键始终是正确的.但除此之外它应该可以工作

                  that way you make sure that key is always the correct on in each ajax success callback. but other than that it should work

                  我用超时代替ajax做了一个小闭包演示,但原理是一样的:

                  i made a small closure demonstration using timeout instead of ajax but the principle is the same:

                  http://jsfiddle.net/KS6q5/

                  这篇关于循环内的 jQuery.ajax()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
                  XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
                  Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
                  How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
                  How do I get the HTTP status code with jQuery?(如何使用 jQuery 获取 HTTP 状态码?)
                  How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)
                  <tfoot id='SQtrW'></tfoot>

                    <bdo id='SQtrW'></bdo><ul id='SQtrW'></ul>

                      <small id='SQtrW'></small><noframes id='SQtrW'>

                    1. <legend id='SQtrW'><style id='SQtrW'><dir id='SQtrW'><q id='SQtrW'></q></dir></style></legend>

                              <tbody id='SQtrW'></tbody>
                          • <i id='SQtrW'><tr id='SQtrW'><dt id='SQtrW'><q id='SQtrW'><span id='SQtrW'><b id='SQtrW'><form id='SQtrW'><ins id='SQtrW'></ins><ul id='SQtrW'></ul><sub id='SQtrW'></sub></form><legend id='SQtrW'></legend><bdo id='SQtrW'><pre id='SQtrW'><center id='SQtrW'></center></pre></bdo></b><th id='SQtrW'></th></span></q></dt></tr></i><div id='SQtrW'><tfoot id='SQtrW'></tfoot><dl id='SQtrW'><fieldset id='SQtrW'></fieldset></dl></div>