【问题标题】:xmlhttp.responseText not outputting to innerHTMLxmlhttp.responseText 不输出到 innerHTML
【发布时间】:2013-03-16 00:46:19
【问题描述】:

我正在尝试熟悉 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>

我不确定我做错了什么。 w3schools 教程并非详尽无遗。我打算买一本书,但我很想学习这些简单的 GET 调用,因为它会让我朝着正确的方向前进。任何建议将不胜感激。

【问题讨论】:

  • 如果你改用这个会发生什么? document.getElementById("myDiv").innerHTML = 'test';
  • 不是问题,但是如果您要编写loadXMLDoc() 代码以便它接受回调,为什么不做所有readyStatestatus 检查之前 调用回调函数?只需在成功将结果作为参数传递时调用它。 (注意:如果您使用更好的缩进方案,您的代码将更易于阅读,因此更易于调试......)
  • 您的页面是否也由http://192.168.0.5/ 提供服务?如果不是,它可能会将其视为跨域请求。
  • w3schools 经常有不正确的信息或不良做法。请参阅 w3fools.com
  • 哦,废话...是的,该页面是本地的。我完全忽略了这个问题。我将跳转到服务器并将页面添加到网络服务器,然后再试一次。

标签: javascript ajax dom innerhtml


【解决方案1】:
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();


            }

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多