【问题标题】:XML Parsing Error: no root element found Location (php+Ajax)XML 解析错误:找不到根元素位置 (php+Ajax)
【发布时间】:2017-09-08 08:06:06
【问题描述】:

我想用一个简单的程序测试 php-Ajax。我正在使用 html 和 php 文件都存储在同一个目录 (/var/www/html) 中。当我单击按钮时,它在控制台中显示以下错误。 XML 解析错误:找不到根元素位置。 ??

html 文件

<html>
    <head>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <title> Ajax testing </title>
        <script type="text/javascript">
            function load(){
                    var xmlhttp = new XMLHttpRequest();
                    xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        document.getElementById("result").innerHTML = this.responseText;
                    }
                };
                xmlhttp.open("GET", "simple.php" , true);
                xmlhttp.send();
            }
        </script>
    </head>
    <body>
        <button onclick="load();">Click here</button>
        <div id="result"> </div>
    </body>
</html>

php 文件

<?php 
    echo "Hello";
?>

这段代码有什么问题?

【问题讨论】:

    标签: javascript php ajax xml


    【解决方案1】:

    这个帖子解释它xmlhttprequest for local files

    来自线程:

    从历史上看,您不能从 JavaScript 查询本地文件(或者不应该被允许,或者有些奇怪)。这将严重破坏安全性。

    只有少数情况下您可以执行此操作,但通常它们涉及需要为您的浏览器设置的特定安全设置,以解除限制或通知当前页面的执行过程,这是被授予的特殊的权利。例如,这在 Firefox 中可以通过编辑属性来实现。在开发浏览器扩展(例如 Chrome 或 FF)时,如果它们请求文件访问权限,通常也可以。

    我刚刚安装了 Apache 服务器并在上面保存了我的 php 文件。我知道这有点矫枉过正,但是嘿,它奏效了:)

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2017-09-22
      • 1970-01-01
      • 2012-08-22
      相关资源
      最近更新 更多