Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.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 xmlhttp.responseText未输出到innerHTML_Javascript_Ajax_Dom_Innerhtml - Fatal编程技术网

Javascript xmlhttp.responseText未输出到innerHTML

Javascript xmlhttp.responseText未输出到innerHTML,javascript,ajax,dom,innerhtml,Javascript,Ajax,Dom,Innerhtml,我正在努力熟悉Ajax,因为我需要在工作中不断地使用它。我正在学习W3Schools教程,尝试使用Apache2服务器。我在服务器上有一个名为ajax_info.txt的文件(在/var/www(ubuntu)下)。我正在调用它,使用Firebug我看到我得到了很好的响应(4&200),但它没有将文件内容输出到DOM。代码如下: <!DOCTYPE html> <html> <head> <script> v

我正在努力熟悉Ajax,因为我需要在工作中不断地使用它。我正在学习W3Schools教程,尝试使用Apache2服务器。我在服务器上有一个名为ajax_info.txt的文件(在/var/www(ubuntu)下)。我正在调用它,使用Firebug我看到我得到了很好的响应(4&200),但它没有将文件内容输出到DOM。代码如下:

<!DOCTYPE html>
<html>
    <head>
        <script>
        var xmlhttp;
        var url = "http://192.168.0.5/ajax_info.txt";

        function loadXMLDoc(url, cfunc) {
            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 = cfunc;
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }
        function myFunction() {
            loadXMLDoc(url, function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                }
            });
        }
        </script>
    </head>
    <body>
        <div id="myDiv">
            <h2>Let AJAX change this text</h2>
        </div>
        <button type="button" onclick="myFunction()">Change Content</button>
    </body>
</html>

var-xmlhttp;
变量url=”http://192.168.0.5/ajax_info.txt";
函数loadXMLDoc(url,cfunc){
if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=cfunc;
open(“GET”,url,true);
xmlhttp.send();
}
函数myFunction(){
loadXMLDoc(url,函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“myDiv”).innerHTML=xmlhttp.responseText;
}
});
}
让AJAX更改此文本
更改内容
我不太清楚我做错了什么。w3schools教程并不详尽。我打算买一本书,但我很想学习这些简单的“接电话”,因为这会让我朝着正确的方向前进。如有任何建议,将不胜感激

函数ajax(x){
function ajax(x) {

            var a;

            if (window.XMLHttpRequest) {
                a = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                a = new ActiveXObject("Microsoft.XMLHTTP");
            } else {
                alert("Browser Dosent Support Ajax! ^_^");
            }


            if (a !== null) {
                a.onreadystatechange = function() {
                    if (a.readyState < 4) {
                        //document.getElementById('cnt').innerHTML = "Progress";
                    } else if (a.readyState === 4) {
                        //respoce recived
                        var res = a.responseText;

                        document.getElementById('center_scrolling_div').innerHTML = res;
                        eval(document.getElementById('center_scrolling_div').innerHTML);
                    }
                };

                a.open("GET", x, true);
                a.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                a.send();


            }

        }
var a; if(window.XMLHttpRequest){ a=新的XMLHttpRequest(); }else if(window.ActiveXObject){ a=新的ActiveXObject(“Microsoft.XMLHTTP”); }否则{ 警报(“浏览器不支持Ajax!^u^”); } 如果(a!==null){ a、 onreadystatechange=函数(){ 如果(a.readyState<4){ //document.getElementById('cnt').innerHTML=“Progress”; }else if(a.readyState==4){ //收到的回复 var res=a.responseText; document.getElementById('center\u scrolling\u div')。innerHTML=res; eval(document.getElementById('center\u scrolling\u div').innerHTML); } }; a、 打开(“获取”,x,真); a、 setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”); a、 send(); } }
如果改用此选项会发生什么?document.getElementById(“myDiv”).innerHTML='test';这不是问题所在,但是如果您要编写
loadXMLDoc()
代码以便它接受回调,为什么不在调用回调函数之前执行所有
readyState
状态检查呢?只要在成功时调用它,并将结果作为参数传递即可。(注意:如果您使用更好的缩进方案,您的代码将更易于阅读,因此更易于调试……)您的页面是否也由
http://192.168.0.5/
?否则,它可能会将其视为跨域请求。W3学校通常有不正确的信息或错误的做法。看w3doubs.com废话…是的,页面是本地的。我完全忽略了那个问题。我将跳转到服务器上,将该页面添加到Web服务器,然后再进行一次尝试。