我是新来的ajax,并试图创建AJAX-> PHP连接。我使用下面的代码PHP ajax呼叫失败
file1.php
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<input type="text" id='demo'>
<input type="button" onclick='ajaxCall()' value='23' >
<script>
function ajaxCall()
{
document.getElementById('demo').value="343434";
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if(xmlhttp.requeststate==4 && xmlhttp.status==200){
document.getElementById('demo').value="4444343434";
document.getElementById('demo').value=xmlhttp.responseText;
}
}
xmlhttp.open("GET","test.php",true);
xmlhttp.send();
}
</script>
</body>
</html>
和相应的test.php现在
<?php
echo "me";
?>
当我按一下按钮,文本框的值更改为343434,但不更改AJAX调用,即使它不会更改为4444343434.我目前正在ubuntu 14.04LTS上运行php 5.5.6。
既然你包含了jquery,可以使用jquery的Ajax方法,而不是用'new XMLHttpRequest'等手工编写Ajax。确实是 – developerwjk
。使用jquery自己的ajax函数。它会添加你缺少的错误处理。你的代码只是假设一切都会成功。 –