weixin_33722405 2014-12-03 20:21 采纳率: 0%
浏览 36

Ajax请求使用循环

Can you help me please to integrate this ajax code in a for loop?

I am trying to get the href from elements with class "links", and then use them to make httpRequests.
I have seen with firebug, that the httpRequests are sended, and i think the problem is somwere in the alertContents function. I struggled all day, but no result.

<a class="links" href="/1">Link1</a>
<a class="links" href="/2">Link2</a>
<a class="links" href="/3">Link3</a>

<script>
for(var i = 0; i < 2; i++) {
makeRequest(document.getElementsByClassName("links")[i].href);
}

function makeRequest(url) {
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
  httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
  try {
    httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  } 
  catch (e) {
    try {
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    catch (e) {}
  }
}

if (!httpRequest) {
  alert('Giving up :( Cannot create an XMLHTTP instance');
  return false;
}
httpRequest.onreadystatechange = alertContents;
httpRequest.open('GET', url);
httpRequest.send();
}

function alertContents() {
if (httpRequest.readyState === 4) {
  if (httpRequest.status === 200) {
    alert(httpRequest.responseText);
   // here i get the final value of the variable i
  } else {
    alert('There was a problem with the request.');
  }
}
}
</script>
  • 写回答

1条回答 默认 最新

  • weixin_33701617 2014-12-03 20:36
    关注

    I declared the variable so it is not global and moved your callback function inside of the makeRequest function.

    for (var i = 0; i < 2; i++) {
        makeRequest(document.getElementsByClassName("links")[i].href);
    }
    
    function makeRequest(url) {
    
        var httpRequest;
    
        function alertContents() {
            if (httpRequest.readyState === 4) {
                if (httpRequest.status === 200) {
                    alert(httpRequest.responseText);
                    // here i get the final value of the variable i
                } else {
                    alert('There was a problem with the request.');
                }
            }
        }
    
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
    
        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = alertContents;
        httpRequest.open('GET', url);
        httpRequest.send();
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据
  • ¥15 multisim中关于74ls192n和DSWPK开关的问题(相关搜索:计数器)
  • ¥15 在误装Windows server2019 后如何利用Windows.old恢复?
  • ¥20 代码实现状态连接包过滤防火墙的设计与实现
  • ¥15 vscode的红色箭头爆红和has no default export报错
  • ¥15 关于#sql#的问题:#情况描述 在用vs对项目进行调试时,出现找不到网络路径,然后查看SQL配置工具,发现SQL服务显示远程调用过程失败(相关搜索:防火墙)