您的位置:首页 > 理论基础 > 计算机网络

ajax的简单实例 XMLHttpRequest对象

2017-05-26 19:11 801 查看
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
window.onload=function(){           //我们经常使用 window.onload 来处理页面,当页面加载完成做一些事情,window.onload = function(){}
document.getElementsByTagName("a")[0].onclick=function(){
var request=new XMLHttpRequest();//新建XMLHttpRequest
var url=this.href;
var method="GET";
request.open(method, url);//open相当于大炮,先打开大炮,里面装着两个参数发射的方式method和发射的目标url
request.send(null);//send 大炮发射命令 参数是炮弹的内容
request.onreadystatechange=function()//onreadystatechange是事件,为XMLHttpRequest对象添加响应函数
{
if (request.readyState==4 && request.status==200)//request的两个属性readyState和status分别为4,200时响应完成响应可用
{
alert(request.responseText)//打印显示结果
}
}
return false;
}

}
</script>
</head>
<body>
<a href="helloajax.txt">hello ajax</a>
</body>
</html>


XMLHttpReques的方法:

open(method,url)

send(content)

事件处理函数:

onreadystatechange





运行结果:

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