001 Ajax中XMLHttpRequest的讲解

时间:2022-04-09 14:42:24

1.介绍

  001 Ajax中XMLHttpRequest的讲解

2.方法

  001 Ajax中XMLHttpRequest的讲解

  001 Ajax中XMLHttpRequest的讲解

3.程序位置设计

  001 Ajax中XMLHttpRequest的讲解

4.程序(针对XMLHttpRequest)

 <%@ 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(){
document.getElementsByTagName("a")[0].onclick=function(){
//new XMLHttpRequest
var request=new XMLHttpRequest(); var url=this.href;
var method="GET"; request.open(method,url);
request.send(null); request.onreadystatechange=function(){
if(request.readyState==4){
if(request.status==200||request.status==304){
alert(request.responseText);
}
}
}
return false;
}
}
</script>
</head>
<body>
<a href="helloAjax.txt">hello click</a>
</body>
</html>

二.解释

1.发送函数

  001 Ajax中XMLHttpRequest的讲解

2.onreadystatechange处理函数

  001 Ajax中XMLHttpRequest的讲解

3.open方法

  001 Ajax中XMLHttpRequest的讲解

三:相应接受

1.接受函数

  001 Ajax中XMLHttpRequest的讲解

2.readState

  001 Ajax中XMLHttpRequest的讲解

3.reponseText方法

  001 Ajax中XMLHttpRequest的讲解

4.reponseXML

  001 Ajax中XMLHttpRequest的讲解

四:添加时间戳

1.url

  var url=this.href+"?time="+new Date();

五:注意点(post方法)

1.知识点

  001 Ajax中XMLHttpRequest的讲解

2.程序

 <%@ 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(){
document.getElementsByTagName("a")[0].onclick=function(){
//new XMLHttpRequest
var request=new XMLHttpRequest(); var url=this.href;
var method="POST"; request.setRequestHeader("ContentType","application/x-www-form-urlencoded"); request.open(method,url);
request.send("name='tom'"); request.onreadystatechange=function(){
if(request.readyState==4){
if(request.status==200||request.status==304){
alert(request.responseText);
}
}
}
return false;
}
}
</script>
</head>
<body>
<a href="helloAjax.txt">hello click</a>
</body>
</html>