首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从javascript多次调用ajax

从javascript多次调用ajax
EN

Stack Overflow用户
提问于 2013-12-27 13:22:37
回答 6查看 1K关注 0票数 1
代码语言:javascript
运行
复制
for(var x=0 ; x<=23 ; x++)
{
    AjaxRequest16 = null;
    AjaxRequest16 = getXmlHttpRequestObject(); // method here to load the request

    if(AjaxRequest16.readyState == 4 || AjaxRequest16.readyState == 0) 
    {
        AjaxRequest16.open("GET", "ajax.php?id=16&AreaID=" +encodeURIComponent(AreaID)+ "&month=" 
                +encodeURIComponent(document.getElementById("cboMonths").value)+ "&TimeSlot=" +encodeURIComponent(x), true);

        AjaxRequest16.send(null);

        AjaxRequest16.onreadystatechange = function()
        {
            if(AjaxRequest16.readyState == 4) 
            {
                var innerHTML = AjaxRequest16.responseText.toString();
                /* Retrieve data from the server and display. */ 
                document.getElementById("divTime"+x).innerHTML = innerHTML;

            }/* end if */            
        }/* end function */            
    }/* end if */            

}/* end if */  

我试图多次调用ajax来加载一组div中的数据:其中24种,它们从divTime0、divTime1、divTime2、divTime3开始.divTime23。每次调用它时,TimeSlot的值都对应于div,例如,TimeSlot=0以divTime0表示。

我知道这里的ajax调用是相互覆盖的,但不知道如何在不编写24块代码的情况下解决它。注:如果不使用for循环执行,这个工作的,但它只填充24个div中的一个

下面的代码用于加载带有图像的24个div:

代码语言:javascript
运行
复制
for(var x=0 ; x<=23 ; x++)
    document.getElementById("timeanalysisimg"+x).src="ajax.php?id=15&AreaID=" +encodeURIComponent(AreaID); 

我试图做一些类似的事情,而不必编写不必要的代码。有什么想法吗?

,我让它工作了,。只是粘贴解决方案

代码语言:javascript
运行
复制
for(var x=0 ; x<=9 ; x++)
{
    test(x, AreaID); // calling the function which resides externally to the loop
}

外部方法:

代码语言:javascript
运行
复制
function test(x, AreaID)
{
        var AjaxRequest16 = null;
        AjaxRequest16 = getXmlHttpRequestObject();

        if(AjaxRequest16.readyState == 4 || AjaxRequest16.readyState == 0) 
        {
            AjaxRequest16.open("GET", "ajax.php?id=16&AreaID=" +encodeURIComponent(AreaID)+ "&month=" 
                    +encodeURIComponent(document.getElementById("cboMonths").value)+ "&TimeSlot=" +encodeURIComponent(x), true);

            AjaxRequest16.send(null);

            AjaxRequest16.onreadystatechange = function()
            {
                if(AjaxRequest16.readyState == 4) 
                {
                    var innerHTML = AjaxRequest16.responseText.toString();
                    /* Retrieve data from the server and display. */ 
                    document.getElementById("divTime"+x).innerHTML = innerHTML;

                }      
            }
        }    
}
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-12-27 13:50:57

将块放入一个函数中:

代码语言:javascript
运行
复制
for(var x=0 ; x<=23 ; x++)
{
  (function(x) {
    var AjaxRequest16 = getXmlHttpRequestObject();
    //rest of the code

  }(x));
} //end of for loop
票数 1
EN

Stack Overflow用户

发布于 2013-12-27 13:32:02

你可以这样做:

代码语言:javascript
运行
复制
for(var x=0 ; x<=23 ; x++)
{
    req(x);

}

function req(x){
    var AjaxRequest16 = null;
    AjaxRequest16 = getXmlHttpRequestObject(); // method here to load the request

    if(AjaxRequest16.readyState == 4 || AjaxRequest16.readyState == 0) 
    {
        AjaxRequest16.open("GET", "ajax.php?id=16&AreaID=" +encodeURIComponent(AreaID)+ "&month=" 
                +encodeURIComponent(document.getElementById("cboMonths").value)+ "&TimeSlot=" +encodeURIComponent(x), true);

        AjaxRequest16.send(null);

        AjaxRequest16.onreadystatechange = function()
        {
            if(AjaxRequest16.readyState == 4) 
            {
                var innerHTML = AjaxRequest16.responseText.toString();
                /* Retrieve data from the server and display. */ 
                document.getElementById("divTime"+x).innerHTML = innerHTML;

            }/* end if */            
        }/* end function */            
    }/* end if */            

}
票数 1
EN

Stack Overflow用户

发布于 2013-12-27 14:07:39

我更改了所有代码,但它完全按照您的要求执行,而不使用asynchronous = false和浏览器冻结:

代码语言:javascript
运行
复制
function ajaxRequest(url, callback) {
    var req = null;
    if (window.XMLHttpRequest) req = new XMLHttpRequest();
    else if (window.ActiveXObject) // if IE
    {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP")
        } catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP")
            } catch (e) {}
        }
    } else {
        throw ("No Ajax support!");
        return;
    }
    req.open('GET', url, true);
    req.onreadystatechange = function () {
        if (req.readyState == 4) {
            if (typeof (callback) == "function") callback(req);
        }
    };
    req.send(null);
    return req;
}

function loadMyData() {
    var x = parseInt(arguments[0]);
    if (x > 23) {
        alert("all 24 is loaded!");
    }
    var url = "ajax.php?id=16&AreaID=" + encodeURIComponent(AreaID) +
        "&month=" + encodeURIComponent(document.getElementById("cboMonths").value) +
        "&TimeSlot=" + encodeURIComponent(x);
    var callback = Function('req', 'document.getElementById("divTime' + x + '").innerHTML =' +
        ' req.responseText;' +
        'loadMyData(' + x + ');');
    ajaxRequest(url, callback);
}

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

https://stackoverflow.com/questions/20801510

复制
相关文章

相似问题

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