weixin_33717298 2014-06-13 20:45 采纳率: 0%
浏览 74

为什么我的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。我目前正在ubuntu14.04LTS上运行php5.5.6。

  • 写回答

1条回答 默认 最新

  • 7*4 2014-06-13 20:53
    关注

    Change xmlhttp.requeststate to xmlhttp.readyState

    Try use the following crossbrowsing function

    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;
    }
    

    And change xmlhttp = new XMLHttpRequest(); to xmlhttp = new getXmlHttp();

    And this work without jQuery.

    评论

报告相同问题?