这是我第一次使用PHP和XMLHttpRequest,我不知道我在哪里搞砸了。
这是我的javascript代码:(newsmanager.js)
function getNews(){
var httpReq;
if (window.XMLHttpRequest){
httpReq=new XMLHttpRequest();
} else {
httpReq=new ActiveXObject("Microsoft.XMLHTTP");
}
try {
httpReq.open("GET", "../news/newsgetter.php", false);
httpReq.onreadystatechange = stateChange;
httpReq.send(null);
} catch(e) {
alert("ERROR: " + e);
}
function stateChange() {
//convert result to JSON
alert(httpReq.readyState);
alert(httpReq.responseText);
var result = JSON.parse(httpReq.responseText);
loadNews(result);
}
}
function loadNews(result){ //its an ARRAY
alert("test 2");
var newsContainer = document.getElementById("news-holder");
var html = '<hr>';
for(var i = 0; i < result.length; i++) {
html += result[i];
html += '<hr>';
}
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代码本身,而不是回显的数组。我已经读了好几遍了,我不明白为什么会引起这个问题。如能提供任何帮助,将不胜感激。
发布于 2014-07-13 13:06:07
您应该启用/配置php来使用您的with服务器。您的not服务器返回您的php代码,因为它不知道应该执行它。查看web,了解如何在OS/Webserver上安装php的教程。
https://stackoverflow.com/questions/24722752
复制相似问题