AJAX 代码框架

Javascript

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
    <title>Ajax Test</title>


<script type="text/javascript">
    
    function getDate()
    {
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        
        if(!xmlhttp)
        {
            alert("Create xml error");
        }
        
// GET 方式如果是固定请求,会从缓存中读取信息,解决缓存问题,可在 URL 后加 "&ts="+new Date(), 每次都会变化的东西
        xmlhttp.open("GET", "https://www.AppAnnie.com", false); // 如果请求的 URL 中含有中文,需要使用 javascript 的一个函数 encodeURI("中文")
      
     xmlhttp.onreadystatechange = function()
        {
        
            if(xmlhttp.readystate==4)
            {
                
                if(xmlhttp.status==200)
                    {
                        alert(xmlhttp.responseText);
                        document.getElementById("showdate").value = xmlhttp.responseText;

                    }
                else
                    {
                        alert("Ajax server error");
                    }
            }

        }

        xmlhttp.send();

    }
</script>

</head>

<body>

<input id="showdate">
<button type="button" onclick="getDate()">Get Date</button>
</body>
</html>
原文地址:https://www.cnblogs.com/TikyZheng/p/4644219.html