2017-04-12 64 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文件。我知道這是一個有點矯枉過正,但嘿,它的工作:)