ajax简单请求示例

2017-06-23 14:23:00
admin
原创
552
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>一个简单的异步请求示例</title>
    
<script type="text/javascript">
var xmlHttpObject;
 
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttpObject = new XMLHttpRequest();
    }
}
    
function startRequest() {
    createXMLHttpRequest();
    xmlHttpObject.onreadystatechange = handleStateChange;
    xmlHttpObject.open("GET", "http://code.ybao.org/demo/src/simpleResponse.txt", true);
    xmlHttpObject.send(null);
}
    
function handleStateChange() {
    if(xmlHttpObject.readyState == 4) {
        if(xmlHttpObject.status == 200) {
            alert("服务器响应为:" + xmlHttpObject.responseText);
        }
    }
}
</script>
</head>
 
<body>
    <form action="#">
        <input type="button" value="发送异步请求" onClick="startRequest();"/>
    </form>
</body>
</html>
  • QQ讨论群
  • 关注微信
    • 欢迎咨询
    • 欢迎加入QQ群 16506021
  • 公众号:源宝网络