Php Ajax在新的Apache服务器上不起作用
我刚刚在Raspberry Pi上安装了一个新的Apache2服务器,但我似乎无法运行Ajax。我想我的代码可能是错的,但这就是为什么我把它贴在这里 这是我的HTML:Php Ajax在新的Apache服务器上不起作用,php,ajax,apache,Php,Ajax,Apache,我刚刚在Raspberry Pi上安装了一个新的Apache2服务器,但我似乎无法运行Ajax。我想我的代码可能是错的,但这就是为什么我把它贴在这里 这是我的HTML: <html> <head> </head> <body> <div id='rotation'>Current Degrees: </div> <br /> <br /> <form action
<html>
<head>
</head>
<body>
<div id='rotation'>Current Degrees: </div>
<br />
<br />
<form action='.' method='get'>
<button button='button' onclick='loadDoc(10)'>Move Left 10°</button><br />
<button button='button' onclick='loadDoc(-10)'>Move Right 10°</button><br />
</form>
<script>
function loadDoc(rot) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById('rotation').innerHTML = "Changed Degrees: " + xhttp.responseText;
}
}
xhttp.open("GET", "run_servo.php?d=10",true);
xhttp.send();
}
</script>
</body>
</html>
当前学位:
向左移动10°
向右移动10°
函数loadDoc(rot){
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
如果(xhttp.readyState==4&&xhttp.status==200){
document.getElementById('rotation').innerHTML=“更改的度数:”+xhttp.responseText;
}
}
open(“GET”,“run\u servo.php?d=10”,true);
xhttp.send();
}
以下是我的PHP:
<?php
$degrees = $_REQUEST['d'];
echo $degrees;
?>
当我运行页面时,我没有得到任何错误,至少没有看到任何错误,但是当我单击按钮时,什么也没有发生。
我检查了Apache日志,没有发现任何问题。为了让它工作,服务器上的所有权限都设置为777。任何关于我做错了什么的见解,或是给我指明了想法的方向吗?我已经找到了答案。编程和AJAX方面的新手错误。在上面的HTML代码中,我将
表单
数据保留在代码中
下面是最后一段有效的HTML代码:
<html>
<head>
</head>
<body>
<div id='rotation'>Current Degrees: </div>
<br />
<br />
<button button='button' onclick='loadDoc(10)'>Move Left 10°</button><br />
<button button='button' onclick='loadDoc(-10)'>Move Right 10°</button><br />
<script>
function loadDoc(rot) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById('rotation').innerHTML = "Changed Degrees: " + xhttp.responseText;
}
}
xhttp.open("GET", "run_servo.php?d=10",true);
xhttp.send();
}
</script>
</body>
</html>
浏览器中的控制台日志中没有任何内容?(代码看起来不错)。如果你选择在PHP端回显任何其他内容,你会得到视觉反馈吗?@Jesse感谢你提醒我关于该日志的事情。我看到它在控制台日志中显示了一个未修改的
HTTP/1.1 304。既然你在查200,那很可能就是答案。然而,我一直认为XHRs会一直跟进,直到它遇到一个真正的错误或一个200I解决了304
错误(请参阅我的答案)。谢谢你的帮助,我会在以后遇到这个问题的。
<?php
header("Cache-Control: no-cache, must-revalidate");
$degrees = $_REQUEST['d'];
echo $degrees;
?>