PHP ::: 2 个 AJAX 调用 ::: 2 个响应 ::: 按顺序

发布于 2024-12-27 09:04:15 字数 327 浏览 0 评论 0原文

这个问题已经被问过好几次了,但已经得到了很好的解决。

在我看来,一个糟糕的实现是:

使用像 mootools 或 jquery 这样的库

在请求之间使用某种延迟,例如 setTimeout("doNext()",3000);

具体来说,我想要实现的目标就是这个。

<div onClick="javascript:one();two();"></div>

one() 和two() 是AJAX

one() 必须出现在two() 之前

This question has been asked several times already, but has been solved with poor implimentations.

A poor implimentation in my opinion is something that:

Uses a library like mootools or jquery

Uses some sort of delay in between requests like setTimeout("doNext()",3000);

Specifically what I am trying to achieve is this.

<div onClick="javascript:one();two();"></div>

one() and two() are AJAX

one() MUST come before two()

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

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

发布评论

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

评论(3

一笔一画续写前缘 2025-01-03 09:04:15

two(); 必须在 one() 中的 ajax 成功处理程序中调用。例如:

html:

<div onClick="javascript:one();"></div>

javascript:

function one() {
    $.ajax({
        ...
        success: function() {
            setTimeout(two,3000);
        }
    });
}

function two() {
    $.ajax({
        ...
    });
}

two(); has to be called in the success handler of the ajax in one(). E.g.:

html:

<div onClick="javascript:one();"></div>

javascript:

function one() {
    $.ajax({
        ...
        success: function() {
            setTimeout(two,3000);
        }
    });
}

function two() {
    $.ajax({
        ...
    });
}
征棹 2025-01-03 09:04:15

试试这个!

function one() {
    $.ajax({
        ...
        aysnc:false,
    });
}

function two() {
    $.ajax({
        ...
        aysnc:false,
    });
}

Try this !!!

function one() {
    $.ajax({
        ...
        aysnc:false,
    });
}

function two() {
    $.ajax({
        ...
        aysnc:false,
    });
}
烟燃烟灭 2025-01-03 09:04:15

我同意 scessor 的观点,即第二个函数应该在第一个函数内执行 - 如果成功的话。这是我的 AJAX 函数,没有任何 jquery 或其他需要的:

    function ajax_call_1(divId, params)
    {
        if (window.XMLHttpRequest){
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else{
            // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                if(divId!='')
                    document.getElementById(divId).innerHTML=xmlhttp.responseText;
                else{
                    // do something else
                }
                ajax_call_2('html_id', 'parameters_to_be_send')
            }
        }
        xmlhttp.open("POST", "script_url", true); //file
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length); //length
        xmlhttp.setRequestHeader("Connection", "close"); 
        xmlhttp.send(params); //parameters
    }

第二个函数是相同的,但没有成功时的函数调用:

        function ajax_call_2(divId, params)
        {
            if (window.XMLHttpRequest){
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{
                // code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    if(divId!='')
                        document.getElementById(divId).innerHTML=xmlhttp.responseText;
                    else{
                        // do something else
                    }
                }
            }
            xmlhttp.open("POST", "script_url", true); //file
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.setRequestHeader("Content-length", params.length); //length
            xmlhttp.setRequestHeader("Connection", "close"); 
            xmlhttp.send(params); //parameters
        }

ajax_call_1 函数参数是:

divId :将要调用的 html 元素的 id保存 AJAX 调用后的结果

params :应与请求一起发送的参数,即 id、名称或您需要发送的某种其他值。

说实话这两个函数的复用性不太好。我正在将它们更改为更加抽象,以便它们可以用于每 2 个同时进行的 AJAX 调用,但目前它们做得很好。

I agree with scessor that the second function should be executed within the first one - on success. Here is my AJAX function, without any jquery or else needed:

    function ajax_call_1(divId, params)
    {
        if (window.XMLHttpRequest){
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else{
            // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                if(divId!='')
                    document.getElementById(divId).innerHTML=xmlhttp.responseText;
                else{
                    // do something else
                }
                ajax_call_2('html_id', 'parameters_to_be_send')
            }
        }
        xmlhttp.open("POST", "script_url", true); //file
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length); //length
        xmlhttp.setRequestHeader("Connection", "close"); 
        xmlhttp.send(params); //parameters
    }

And the second function is the same, but without a function call on success:

        function ajax_call_2(divId, params)
        {
            if (window.XMLHttpRequest){
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{
                // code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    if(divId!='')
                        document.getElementById(divId).innerHTML=xmlhttp.responseText;
                    else{
                        // do something else
                    }
                }
            }
            xmlhttp.open("POST", "script_url", true); //file
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.setRequestHeader("Content-length", params.length); //length
            xmlhttp.setRequestHeader("Connection", "close"); 
            xmlhttp.send(params); //parameters
        }

ajax_call_1 function arguments are:

divId : id of the html element that will hold the result after the AJAX call

params : parameters that should be sent with the request i.e. id, name or some sort of other values you need to send.

To be honest this 2 functions are not very reusable. I am in progress of changing them to be more abstract so they can be used for every 2 simultaneously made AJAX calls, but for now they do a good job.

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