您的位置:首页 > Web前端 > JavaScript

javascript 实现 ajax 示例代码----get方式

2013-07-22 23:19 537 查看
<html>
<head>
<script type="text/javascript">

var xmlHttpRequest = null;//声明一个空对对象以接受XMLHttpRequest 对象

function ajaxSubmit() {

if(window.ActiveXObject) {

xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器

} else if (window.XMLHttpRequest){

xmlHttpRequest = new XMLHttpRequest();//非IE浏览器
}

if(null != xmlHttpRequest) {

xmlHttpRequest.open("GET", "AjaxServlet", true);
//关联好回调函数
xmlHttpRequest.onreadystatechange = ajaxCallBack;
//向服务器发送数据,发送请求提的内容get 请求时,为null
xmlHttpRequest.send(null);

}
}
function ajaxCallBack() {

if(xmlHttpRequest.readyState == 4) {

if(xmlHttpRequest.status == 200) {

var responseText = xmlHttpRequest.responseText;

document.getElementById("div1").innerHTML = responseText;
}
}
}
</script>
</head>

<body>
<input type="button" value="get value from servlet" onclick="ajaxSubmit()">
<div id="div1"></div>
</body>
</html>




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: