Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于将值从2个服务器端脚本返回到2个DOM目标的ajax函数_Javascript_Ajax - Fatal编程技术网

Javascript 用于将值从2个服务器端脚本返回到2个DOM目标的ajax函数

Javascript 用于将值从2个服务器端脚本返回到2个DOM目标的ajax函数,javascript,ajax,Javascript,Ajax,我想做什么 tizag示例在onreadystatechange函数中更改了DOM,如下所示: if(ajaxRequest.readyState == 4){ document.myForm.time.value = ajaxRequest.responseText; } 但是我想更改多个DOM目标中的值,所以我的想法是尝试返回一个值。我修改了代码,但显然不起作用 下面是我改编的完整代码。您可以在底部看到我尝试访问两个不同脚本的位置。这有可能吗 function ajaxFuncti

我想做什么 tizag示例在onreadystatechange函数中更改了DOM,如下所示:

if(ajaxRequest.readyState == 4){
    document.myForm.time.value = ajaxRequest.responseText;
}
但是我想更改多个DOM目标中的值,所以我的想法是尝试返回一个值。我修改了代码,但显然不起作用

下面是我改编的完整代码。您可以在底部看到我尝试访问两个不同脚本的位置。这有可能吗

function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            return ajaxRequest.responseText;
        }
    }
    ajaxRequest.open("GET", "/time.php", true);
    var time = ajaxRequest.send(null);  // trying to get value from 1st script
    ajaxRequest.open("GET", "/date.php", true);
    var date = ajaxRequest.send(null); // trying to get values from 2nd script

    document.getElementById('time').innerHTML = time; // send 1st variable to DOM
    document.getElementById('date').innerHTML = date; // send 2nd variable to DOM
}

我认为对ajax.open的调用中的第三个参数表示请求是异步执行的,这意味着来自ajax.send调用的返回值将不是来自服务器的脚本的结果

您应该在附加到onreadystatechange的匿名函数体中设置局部变量。您可能可以检查ajax对象以确定返回的服务器脚本

创建两个不同的ajax对象可能更容易,一个用于请求获取日期,另一个用于请求获取时间

我还建议研究jquery,它为您提供了很多管道,并提供了一种更简单的执行ajax请求的方法