Ajax之get、post实例(最原始的XMLHttpServlet)
2014-12-23 15:35
453 查看
2014年12月23日15:36:10 天气阴 心情极度低落
ajaxGet2014122301.jsp
ajaxPost2014122301.jsp
对于post请求必须执行完request.setRequestHeader("ContentType", "application/x-www=form-urlencoded")而后再执行request.send("name='atguigu'");,否则异步提交不起作用。
ajaxGet2014122301.jsp
<%@ 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>Insert title here</title> <script type="text/javascript"> window.onload = function() { //1.获取a节点,并为其添加onclick响应函数 document.getElementsByTagName("a")[0].onclick = function() { //3.创建一个XMLHttpRequest对象 var request = new XMLHttpRequest(); //4.准备发送请求的数据: url var url= this.href + "?time = " + new Date(); var method = "GET"; //5.调用XMLHttpRequest 对象的open方法 request.open(method,url); ///6.调用XMLHttpRequest 对象的send方法 request.send(null); //7.为XMLHttpRequest对象添加onreadystatechange响应函数 request.onreadystatechange = function() { //8.判断响应是否完成:XMLHttpRequest对象的readyState 属性为4的时候 if (request.readyState == 4) { //9.再判断响应是否可用:XMLHttpRequest 对象的 status 为200 if (request.status == 200 || request.status == 304) { //10.打印响应结果:responseText; alert(request.responseText); } } } //2.取消a节点的默认行为 return false; } } </script> </head> <body> <a href="HelloAjax.txt">helloAjax2014122301</a> </body> </html>
ajaxPost2014122301.jsp
<%@ 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>Insert title here</title> <script type="text/javascript"> window.onload = function() { document.getElementsByTagName("a")[0].onclick = function() { var request = new XMLHttpRequest(); var url= this.href + "?time = " + new Date(); var method = "POST"; request.open(method,url); request.setRequestHeader("ContentType", "application/x-www=form-urlencoded") request.send("name='atguigu'"); 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">helloAjax2014122301</a> </body> </html>需要注意的地方是
对于post请求必须执行完request.setRequestHeader("ContentType", "application/x-www=form-urlencoded")而后再执行request.send("name='atguigu'");,否则异步提交不起作用。
相关文章推荐
- 用AJAX的Get和Post调用Servlet的简单示例。
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- jQuery Ajax 实例 ajax提交,post提交,get提交
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- JS中的Ajax操作实例,get方式和post方式对比
- 原始ajax通过xmlHttpRequest对象的send()方法提交数据--Get方式和Post方式
- jQuery调用AJAX时Get和post公用的乱码解决方法实例说明
- Ajax提交数据(Get Post)详解和实例
- jQuery - AJAX get() 和 post() 方法---非常好的实例
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- 用AJAX的Get和Post调用Servlet的简单示例
- ajax 实现 GET POST 异步 同步 检验mysql连接情况简单实例
- Jquery和 LigerUI 用Post\Get\Ajax调用数据前台实例!
- ajax(一) 文本格式操作 get和post 实例
- (转)用AJAX的Get和Post调用Servlet的简单示例。
- jQuery Ajax(load,post,get,ajax) 实例 全解析
- AJAX简单实例(GET、POST)
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- web 开发之js---ajax 中的两种提交方式ajax post 和 ajax get 实例
- jQuery Ajax 实例 ($.ajax、$.post、$.get)