AJAX 简单例程示例

  • index.html

    <html>
    <head>    
    <script>
    function showHint(str)
    {
        if (str.length==0)
        {
            return;
        }
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();   // 申请一个  XMLHttpRequest 的 对象
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                //document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                console.log(xmlhttp.responseText);
            }
        }
        xmlhttp.open("GET","gethint.php?q="+str,true);    // 打开,并传递一个数据过去,通过 GET 的方式
        xmlhttp.send();        // 发送
    }
    </script>
    
    <button onclick="showHint('date')"> test it </button>
    
    </head>
    <body>
    </body>
    </html>
  • gethint.php

      1 <?php
      2
      3 $q=$_GET["q"];   // 这里获取到上面的数据
      4
      5 echo $q;
      6 ?>

猜你喜欢

转载自www.cnblogs.com/chenfulin5/p/9052710.html