Javascript XML分析错误:未找到根元素位置(php+;Ajax)
我想用一个简单的程序测试PHPAJAX。我使用的html和php文件都存储在同一个目录(/var/www/html)中。当我点击按钮时,它在控制台中显示以下错误。 XML分析错误:找不到根元素位置。 ? html文件Javascript XML分析错误:未找到根元素位置(php+;Ajax),javascript,php,ajax,xml,Javascript,Php,Ajax,Xml,我想用一个简单的程序测试PHPAJAX。我使用的html和php文件都存储在同一个目录(/var/www/html)中。当我点击按钮时,它在控制台中显示以下错误。 XML分析错误:找不到根元素位置。 ? html文件 <html> <head> <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> <meta content="utf-
<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
echo "Hello";
?>
Ajax测试
函数加载(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“结果”).innerHTML=this.responseText;
}
};
open(“GET”,“simple.php”,true);
xmlhttp.send();
}
点击这里
php文件
<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
echo "Hello";
?>
这个代码有什么问题 这篇文章解释了这一点
从线程:
从历史上看,您不能从JavaScript查询本地文件(或者不允许查询,或者有些奇怪的事情)。这将是对安全的严重破坏
只有少数情况下您可以这样做,但通常情况下,它们涉及需要为浏览器设置的特定安全设置,以取消限制或通知当前页面的执行过程,即授予此例外权利。例如,在Firefox中,通过编辑属性就可以做到这一点。在开发浏览器扩展名(例如Chrome或FF)时,如果他们请求文件访问权限,通常也可以
我刚刚安装了Apache服务器,并在上面保存了我的php文件。我知道这有点过分了,但嘿,它奏效了:)