PHP ajax调用失败

时间:2014-06-13 20:45:02

标签: javascript php ajax

我是ajax的新手,并尝试创建AJAX-> PHP连接。我使用以下代码

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";
?>

现在,当我单击按钮时,textbox的值将更改为343434但不会更改AJAX调用,即使它不会更改为4444343434.我目前正在ubuntu 14.04LTS上运行php 5.5.6。

1 个答案:

答案 0 :(得分:1)

xmlhttp.requeststate更改为xmlhttp.readyState

尝试使用以下交叉浏览功能

function getXmlHttp(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

xmlhttp = new XMLHttpRequest();更改为xmlhttp = new getXmlHttp();

这项工作没有jQuery。