请求两个 Ajax

发布于 2024-12-28 02:22:23 字数 1832 浏览 2 评论 0原文

我正在尝试进行两次 Ajax 调用来获取数据以填充网页的不同部分,正如您已经知道的那样,只会发生第二次。

所以我想我应该这样做:

callAjax1('a'); callAjax2('b');

function callAjax1(data) {
 ajax(data);
}

function callAjax2(data) {
 ajax(data);
}

function ajax(data) {
 // calls XMLHttpRequestObject etc
}

这个想法是,现在我将拥有两个可以独立运行的独立 ajax 实例,而不是调用 ajax() 两次。

它有效..但前提是我在 ajax() 顶部添加警报以让我知道我已经到达。

所以我认为警报会在调用第二个请求之前给第一个请求时间来完成。因此,我没有设法将它们正确地分成单独的实例。那不可能吗?

我缺少什么?

一切顺利 更新

: 我在想,我还有机会吗?

tParams = new Array (2); // we intend to call ajax twice
tParams[0] = new Array('ajaxGetDataController.php', 'PROJECT', 'id');
tParams[1] = new Array('ajaxGetFileController.php', 'FILE', 'projectId');

<select name='projectSelector' onchange=\"saveData(tParams, this.value);\">\n";

// gets called, twice
function saveData(pParams, pData) // pParams are: PageToRun, Table, Field
{
    if (XMLHttpRequestObject)
    {
        tPage = pParams[0][0]+'?table='+pParams[0][1]+'&pField='+pParams[0][2]+'&pData='+pData;
        XMLHttpRequestObject.open('GET', tPage);\n

        XMLHttpRequestObject.onreadystatechange = callAjax(pParams, pData);

        XMLHttpRequestObject.send(null);

    }   
}

function callAjax(pParams, pData)
{
 if (XMLHttpRequestObject.readyState == 4 &&    XMLHttpRequestObject.status == 200)
    {
        var tReceived = XMLHttpRequestObject.responseXML;
        options = tReceived.getElementsByTagName('option'); // fields and their values stored in simplest XML as options
        popForm(options, pParams[0][1]); // goes off to use the DOM to populate the onscreen form
        pParams.shift();    // cuts off pParams[0] and moves all elements up one
        if (pParams.length>0)
        {
            saveData(pParams, pData);
        }
    }
}

I'm trying to make two Ajax calls to get data to populate different bits of a web page, and as you'll already know, only the second happens.

So I thought I'd do this:

callAjax1('a'); callAjax2('b');

function callAjax1(data) {
 ajax(data);
}

function callAjax2(data) {
 ajax(data);
}

function ajax(data) {
 // calls XMLHttpRequestObject etc
}

The idea was that instead of calling ajax() twice, now, I'd have two independent instances of ajax that would run independently.

It works .. but only if I put in an alert at the top of ajax() to let me know I've arrived.

So I'm thinking that alert gives the first request time to finish before the second is called. Therefore, I've not managed to separate them properly into separate instances. Is that not possible?

What am I missing?

All the best
J

UPDATE:
I'm thinking this, do I stand a chance?

tParams = new Array (2); // we intend to call ajax twice
tParams[0] = new Array('ajaxGetDataController.php', 'PROJECT', 'id');
tParams[1] = new Array('ajaxGetFileController.php', 'FILE', 'projectId');

<select name='projectSelector' onchange=\"saveData(tParams, this.value);\">\n";

// gets called, twice
function saveData(pParams, pData) // pParams are: PageToRun, Table, Field
{
    if (XMLHttpRequestObject)
    {
        tPage = pParams[0][0]+'?table='+pParams[0][1]+'&pField='+pParams[0][2]+'&pData='+pData;
        XMLHttpRequestObject.open('GET', tPage);\n

        XMLHttpRequestObject.onreadystatechange = callAjax(pParams, pData);

        XMLHttpRequestObject.send(null);

    }   
}

function callAjax(pParams, pData)
{
 if (XMLHttpRequestObject.readyState == 4 &&    XMLHttpRequestObject.status == 200)
    {
        var tReceived = XMLHttpRequestObject.responseXML;
        options = tReceived.getElementsByTagName('option'); // fields and their values stored in simplest XML as options
        popForm(options, pParams[0][1]); // goes off to use the DOM to populate the onscreen form
        pParams.shift();    // cuts off pParams[0] and moves all elements up one
        if (pParams.length>0)
        {
            saveData(pParams, pData);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

子栖 2025-01-04 02:22:23

我将为 AJAX 函数创建一个就绪状态变量:

function ajax(data) {
 readyState = false;
 // calls XMLHttpRequestObject etc
}

然后在执行第二个调用之前检查就绪状态:

function callAjax2(data) {
if(readyState == true) {
 ajax(data);
 readyState = true;
 }
}

并确保在执行 AJAX 调用后将 ReadyState 更改回 false。这将确保第一个 AJAX 调用在第二个 AJAX 调用尝试触发之前完成执行。

I would create a ready state variable for the AJAX function:

function ajax(data) {
 readyState = false;
 // calls XMLHttpRequestObject etc
}

And then check for the ready state before executing the second call:

function callAjax2(data) {
if(readyState == true) {
 ajax(data);
 readyState = true;
 }
}

And make sure to change the readyState back to false after the AJAX calls have executed. This will ensure the first AJAX call has finished executing before the second one tries to fire.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文