Java XMLHttpRequest返回PHP代码,而不是PHP输出

Java XMLHttpRequest返回PHP代码,而不是PHP输出,java,php,json,xmlhttprequest,Java,Php,Json,Xmlhttprequest,这是我第一次使用PHP和XMLHttpRequest,我不知道我把事情搞砸了 以下是我的javascript代码:(newsmanager.js) 函数getNews(){ var-httpReq; if(window.XMLHttpRequest){ HttpRequest=新的XMLHttpRequest(); }否则{ httpReq=新的ActiveXObject(“Microsoft.XMLHTTP”); } 试一试{ open(“GET”,“./news/newsgetter.php

这是我第一次使用PHP和XMLHttpRequest,我不知道我把事情搞砸了

以下是我的javascript代码:(newsmanager.js)

函数getNews(){ var-httpReq; if(window.XMLHttpRequest){ HttpRequest=新的XMLHttpRequest(); }否则{ httpReq=新的ActiveXObject(“Microsoft.XMLHTTP”); } 试一试{ open(“GET”,“./news/newsgetter.php”,false); httpReq.onreadystatechange=stateChange; httpReq.send(空); }捕获(e){ 警报(“错误:+e”); } 函数stateChange(){ //将结果转换为JSON 警报(httpReq.readyState); 警报(httpReq.responseText); var result=JSON.parse(httpReq.responseText); 加载新闻(结果); } } 函数loadNews(result){//它是一个数组 警报(“测试2”); var newscanner=document.getElementById(“新闻持有者”); var html=“
”; 对于(变量i=0;i”; } newsContainer.innerHTML=html; } 下面是我的PHP代码:

<?php

  //folder relative to this file
  $dir = "messages";
  //array holding all addresses
  $result = array();

  //get files in directory
  $files = scandir($dir);

  while (($file = $dir->read()) !== false){
    //find MD files
    if(strlen($file) < 3 || substr($file, -4) != '.md')
        continue;

    $result[] = $dir . "/" . $file; 
  }

  //convert array to JSON
  echo(json_encode($result));

?>


问题是httpReq.responseText返回的是PHP代码本身,而不是echo中的数组。我已经读了好几遍了,我不明白为什么会出现这个问题。任何帮助都将不胜感激

您应该启用/配置php以与您的Web服务器一起工作。 您的Web服务器返回您的php代码,因为它不知道应该执行它。
在web上查找有关如何在OS/web服务器上安装php的教程。

您的web服务器正在运行吗?是。主机是Github页面
<?php

  //folder relative to this file
  $dir = "messages";
  //array holding all addresses
  $result = array();

  //get files in directory
  $files = scandir($dir);

  while (($file = $dir->read()) !== false){
    //find MD files
    if(strlen($file) < 3 || substr($file, -4) != '.md')
        continue;

    $result[] = $dir . "/" . $file; 
  }

  //convert array to JSON
  echo(json_encode($result));

?>