发送由单一的JavaScript函数中的两个Ajax请求到两个不同的PHP脚本 [英] Sending two Ajax requests to two different PHP scripts from single javascript function

查看:72
本文介绍了发送由单一的JavaScript函数中的两个Ajax请求到两个不同的PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能Ajax请求同时发送到两个或更多的PHP脚本?我知道,这是可以实现连续(获得1反应,然后让来自其它响应),但我想知道是否有可能同时进行。请帮助我用下面的code:

 函数calShowUpload(){
    如果(http.readyState == 4){
        RES = http.responseText;
        。的document.getElementById('日程地址搜索Maincontent)的innerHTML =资源;
    }
}

功能showUpload(OBJ)
{
      http.open(GET,./showUpload.php,真正的);
    http.onreadystatechange = calShowUpload;
    http.send(空);
}


传播getXHTTP(){
    VAR xhttp;
    尝试{//下面的尝试块获取XMLHTTP对象的各种浏览器...
            xhttp =新的ActiveXObject(MSXML2.XMLHTTP);
        }
    赶上(E){
            尝试 {
            xhttp =新的ActiveXObject(Microsoft.XMLHTTP);
            }
        赶上(E2){
         //此块处理的Mozilla / Firefox浏览器...
                尝试 {
                    xhttp =新XMLHtt prequest();
                }
            赶上(E3){
                    xhttp = FALSE;
                }
            }
        }
    返回xhttp; //返回XMLHTTP对象
}

变种的http = getXHTTP(); //这个执行第一次加载页面的时候。
 

在上面的例子中,我知道我可以调用另一个函数,如 showUpload(OBJ) calShowUpload()实现我的目标,但我需要的是这样的:

 函数showUpload(OBJ)
{
      http.open(GET,./showUpload.php,真正的);
      http.open(GET,./showDelete.php,真正的);
    http.onreadystatechange = calShowUpload;
    http.send(空);
}
 

解决方案

您需要的XMLHtt prequest两个实例或第二个会跺脚第一。非常,非常简单的方式与您现有的code做到这一点很简单,就是创建两个实例,写你的JS使用哪一个是合适的任务。

  VAR HTTP1 = getXHTTP(); //使用此一请求。
变种http2 = getXHTTP(); //使用这个为其他请求。
 

Is it possible to send Ajax requests to two or more Php scripts simultaneously? I know that this can be achieved serially (getting response from 1 and then getting response from the other) but I am wondering is it possible simultaneously. Please assist me with the following code:

function calShowUpload(){
    if (http.readyState == 4) {
        res = http.responseText;
        document.getElementById('maincontent').innerHTML=res;
    }
}

function showUpload(obj)
{
      http.open("GET", "./showUpload.php", true);
    http.onreadystatechange = calShowUpload;
    http.send(null);
}


function getXHTTP() {
    var xhttp;
    try {   // The following "try" blocks get the XMLHTTP object for various browsers…
            xhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
    catch (e) {
            try {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } 
        catch (e2) {
         // This block handles Mozilla/Firefox browsers...
                try {
                    xhttp = new XMLHttpRequest();
                } 
            catch (e3) {
                    xhttp = false;
                }
            }
        }
    return xhttp; // Return the XMLHTTP object
}

var http = getXHTTP(); // This executes when the page first loads.

In the above example I know that I can call another function like showUpload(obj) inside calShowUpload() to achieve my objective, but I need something like this:

function showUpload(obj)
{
      http.open("GET", "./showUpload.php", true);
      http.open("GET", "./showDelete.php", true);
    http.onreadystatechange = calShowUpload;
    http.send(null);
}

解决方案

You need two instances of the XMLHttpRequest or the second will stomp the first. The very, very easiest way to do this with your existing code is simply to create two instances and write your JS to use whichever one is appropriate to the task.

var http1 = getXHTTP(); // Use this for one request.
var http2 = getXHTTP(); // Use this for the other request.

这篇关于发送由单一的JavaScript函数中的两个Ajax请求到两个不同的PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆