2017-04-12 65 views
0

我想测试PHP阿贾克斯用一个简单的program.I我使用HTML和PHP文件都存储在同一目录(/ var/www/html等) 。当我点击按钮,它显示了在控制台下面的错误。 XML解析错误:没有根元素中找到位置。 ??XML解析错误:没有根元素中找到位置(PHP +阿贾克斯)

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"; 
?> 

什么是错的代码?

回答

0

此线程解释它xmlhttprequest for local files

从螺纹:

Historically, you can't query for local files from JavaScript (or shouldn't be allowed to, or something's odd). This would be a serious breach of security.

There are only a few circumstances where you can do this, but in general they involve specific security settings requiring to be set for your browser, to either lift the limitation or to notify the current page's execution process that that is is granted this exceptional right. This is for instance doable in Firefox by editing the properties. It's also commonly OK when developing browser extensions (for instance for Chrome or FF) if they request the file access permissions.

我刚安装的Apache服务器,并保存在我的PHP文件。我知道这是一个有点矫枉过正,但嘿,它的工作:)