在php中使用ajax向服务器发送请求

时间:2014-05-08 05:28:29

标签: php ajax xmlhttprequest

我使用ajax向服务器发送请求但它没有从php页面获得任何响应。     我很困惑我的代码中出了什么问题?

我的Index.php页面是: -

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        echo 'hello';
        ?>
    </body>
</html>

我的html页面是: -                        

    function showHint()
{

var xmlhttp;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("name").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","index.php",true);
xmlhttp.send();
}
    </script>


</head>
<body>
<input type="text" name="name" id="name" />
<input type="button" name="submit" value="Correct the Address" id="submit" onclick="showHint()"/>

</body>
</html>

我的上述代码有什么问题?请给我一些解决方案?

1 个答案:

答案 0 :(得分:1)

您有语法错误showHint()所以请检查

<input type="button" name="submit" value="Correct the Address" id="submit" onclick="showHint()"/></td>
相关问题