• <tfoot id='bAiXG'></tfoot>

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

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

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

        循环中的 XMLHttpRequest

        时间:2023-05-15

            <legend id='3Ll2Y'><style id='3Ll2Y'><dir id='3Ll2Y'><q id='3Ll2Y'></q></dir></style></legend>

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

            <small id='3Ll2Y'></small><noframes id='3Ll2Y'>

              <tbody id='3Ll2Y'></tbody>
              • <bdo id='3Ll2Y'></bdo><ul id='3Ll2Y'></ul>
                • 本文介绍了循环中的 XMLHttpRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文章

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

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

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

                • <tfoot id='uduM9'></tfoot>