关于Ajax异步XMLHttpRequest对象,你该了解这些。

标签:变化 页面 let javascrip 建立 rac doctype cep test
在这里插入图片描述
示例Ajax:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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=utf-8">
<title>Test Ajax</title>
</head>
<body>

<div style="text-align: center;">
	<button onclick="loadName()">测试Ajax</button>
</div>

</body>
<script type="text/javascript">
function loadName() {
	var xmlHttp;
	if(window.XMLHttpRequest){
		xmlHttp=new XMLHttpRequest();
	}else{//IE 5,6的支持
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	//xmlHttp.open("get", "testAjax", true);
	//xmlHttp.open("post", "testAjax", true);
	
	/*
	//get请求
	xmlHttp.open("get", "testAjax?name=Anner&age=24", true);
	xmlHttp.send();
	*/
	//post请求
	xmlHttp.open("post", "testAjax", true);
	xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlHttp.send("name=Anner&age=24");
}
</script>
</html>

XMLHttpRequset对象相应服务器

onreadystatechange事件

当请求被发送到服务器时,我们需要执行一些基于响应的任务

当readystate改变时,就会触发onreadystatechange事件

XMLHttpRequest对象的三个重要的属性:

1、onreadystatechange存储函数或函数名,每当readyState属性改变时,就调用该函数

readyState存有XMLHttpRequest的状态,从0到4发生变化

0:请求未初始化

1:服务器连接已经建立

2:请求已接收

3:请求处理中

4:请求已完成,且相应已就绪

status:

200:OK

404:未找到页面

如需获得来自服务器的相应,使用XMLHttpRequest对象的responseText或responseXML属性

responseText获得字符串形式的响应数据

responseXML获得XML形式的响应数据

Ajax返回后台Servlet数据

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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=utf-8">
<title>Test Ajax</title>
</head>
<body>

<div style="text-align: center;">
	<button onclick="loadName()">测试Ajax</button>
	<input type="text" name="te" id="te">
</div>

</body>
<script type="text/javascript">
function loadName() {
	var xmlHttp;
	if(window.XMLHttpRequest){
		xmlHttp=new XMLHttpRequest();
	}else{//IE 5,6的支持
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	//xmlHttp.open("get", "testAjax", true);
	//xmlHttp.open("post", "testAjax", true);
	
	/*
	//get请求
	xmlHttp.open("get", "testAjax?name=Anner&age=24", true);
	xmlHttp.send();
	*/
	
	//alert("readyState:"+xmlHttp.readyState+"status状态"+xmlHttp.status);
	xmlHttp.onreadystatechange=function(){
		//alert("readyState:"+xmlHttp.readyState+"status状态"+xmlHttp.status);
		if(xmlHttp.readyState==4 && xmlHttp.status==200){
			//alert(xmlHttp.responseText);
			document.getElementById("te").value=xmlHttp.responseText;
		}
	};
	//post请求
	xmlHttp.open("post", "testAjax", true);
	xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlHttp.send("name=Anner&age=24");
	
	
}
</script>
</html>

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	//req.setCharacterEncoding("utf-8");
	//String name=req.getParameter("name");
	//String age=req.getParameter("age");
	//System.out.println(name+" "+age);
	resp.setContentType("text/html;charset=utf-8");
	PrintWriter out =resp.getWriter();
	out.println("ajax返回");
	out.flush();
	out.close();
}

原文地址:http://www.tofacebook.com/post/17746

本文由tofacebook.com作者上传并发布,百家号仅提供信息发布平台。文章仅代表作者个人观点,不代表百度立场。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值