AJAX示例
<scripttype="text/javascript">
window.onload= function() {
varxhr;
if(window.XMLHttpRequest) {
//IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
xhr= new XMLHttpRequest();
}else {
//IE6, IE5 浏览器执行代码
xhr= new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open("get","testAjax.txt");
alert(xhr.readyState);
xhr.send();
xhr.onreadystatechange= function() {
alert(xhr.readyState);
if(xhr.readyState == 4 && xhr.status == 200) { //返回完整,请求成功
var res =xhr.responseText; //获取返回值
//拿到返回值后可对dom操作,局部刷新页面
alert(res);
}
}
}
</script>