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

      <tfoot id='NSXgi'></tfoot>

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

    1. <legend id='NSXgi'><style id='NSXgi'><dir id='NSXgi'><q id='NSXgi'></q></dir></style></legend>
        <i id='NSXgi'><tr id='NSXgi'><dt id='NSXgi'><q id='NSXgi'><span id='NSXgi'><b id='NSXgi'><form id='NSXgi'><ins id='NSXgi'></ins><ul id='NSXgi'></ul><sub id='NSXgi'></sub></form><legend id='NSXgi'></legend><bdo id='NSXgi'><pre id='NSXgi'><center id='NSXgi'></center></pre></bdo></b><th id='NSXgi'></th></span></q></dt></tr></i><div id='NSXgi'><tfoot id='NSXgi'></tfoot><dl id='NSXgi'><fieldset id='NSXgi'></fieldset></dl></div>

        循环中的 XMLHttpRequest

        时间:2023-10-14

          <tfoot id='tGb4N'></tfoot>

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

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

                  问题描述

                  我正在尝试在 for 循环中发出多个服务器请求.我发现 this question 并实施了建议的解决方案.但是它似乎不起作用.

                  I am trying to make several server requests inside a for loop. I found this question and implemented the suggested solution. However it doesn't seem to work.

                      for (var i = 1; i <= 10; i++)
                      {
                      (function(i) {
                      if(<some conditions>)
                      {
                      if (window.XMLHttpRequest) {
                          // code for IE7+, Firefox, Chrome, Opera, Safari
                          xmlhttp[i]=new XMLHttpRequest();
                        } else { // code for IE6, IE5
                          xmlhttp[i]=new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        xmlhttp[i].onreadystatechange=function() {
                          if (xmlhttp[i].readyState==4 && xmlhttp[i].status==200) {
                            document.getElementById("preselection").innerHTML=xmlhttp[i].responseText;
                          }
                        }
                        xmlhttp[i].open("GET","getBuoys.php?q="+i,true);
                        xmlhttp[i].send();
                      }
                  })(i);
                  }
                  

                  如果我删除 for 循环并将所有 xmlhttp[i] 更改为 xmlhttp,对于一个元素来说一切正常,但我无法发出多个请求.提前感谢您的任何建议.

                  If I remove the for loop and change all xmlhttp[i] to xmlhttp, everything works just fine for one element, but I can't make several requests. Thanks in advance for any suggestions.

                  推荐答案

                  试试下面的代码片段

                  // JavaScript
                  window.onload = function(){
                  
                      var f = (function(){
                          var xhr = [], i;
                          for(i = 0; i < 3; i++){ //for loop
                              (function(i){
                                  xhr[i] = new XMLHttpRequest();
                                  url = "closure.php?data=" + i;
                                  xhr[i].open("GET", url, true);
                                  xhr[i].onreadystatechange = function(){
                                      if (xhr[i].readyState === 4 && xhr[i].status === 200){
                                          console.log('Response from request ' + i + ' [ ' + xhr[i].responseText + ']'); 
                                      }
                                  };
                                  xhr[i].send();
                              })(i);
                          }
                      })();
                  
                  };
                  
                  // PHP [closure.php]
                  echo "Hello Kitty -> " . $_GET["data"];
                  

                  回应

                  Response from request 0 [ Hello Kitty -> 0]
                  Response from request 1 [ Hello Kitty -> 1]
                  Response from request 2 [ Hello Kitty -> 2] 
                  

                  这篇关于循环中的 XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:XMLHttpRequest 浏览器支持 下一篇:如何获取“数据"来自 xhr.responseText 的字段?

                  相关文章

                  最新文章

                  <legend id='bYrYj'><style id='bYrYj'><dir id='bYrYj'><q id='bYrYj'></q></dir></style></legend>
                • <small id='bYrYj'></small><noframes id='bYrYj'>

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

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