ajx技术解析以及模拟jQuery封装

时间:2022-02-10 03:51:49

1.后台处理程序

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password"); System.out.println(username+"=========="+password); out.print("响应内容-->姓名:" +username+",密码:"+password);
%>

2.get方式请求

 <%@ 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>Ajax实现前后台交互</title>
</head>
<body>
<form action="#" method="">
<input type="text" name="username" id="username" />
<input type="password" name="password" id="password" />
<input type="button" value="ajax登录" onclick="tzLogin();" />
</form> <div id="result"></div>
<script type="text/javascript"> function tzLogin(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var params = "?username="+username+"&password="+password+"&method=get"; //创建一个Ajax对象
var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
document.getElementById("result").innerText = this.responseText;
} };
//true代表异步
xhr.open("get","login.jsp"+params,true);
xhr.send();
}
</script>
</body>
</html>

3.post方式请求

 <%@ 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>Ajax实现前后台交互</title>
</head>
<body>
<form action="#" method="">
<input type="text" name="username" id="username" />
<input type="password" name="password" id="password" />
<input type="button" value="ajax登录" onclick="tzLogin();" />
</form> <div id="result"></div>
<script type="text/javascript"> function tzLogin(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var params = "username="+username+"&password="+password+"&method=post"; //创建一个Ajax对象
var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
document.getElementById("result").innerText = this.responseText;
} };
xhr.open("post","login.jsp",true);
//发送数据,如果请求方式是post话send一定要设置参数
//如果get xhr.send(); 如果post send一定要设置参数
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(params);
}
</script>
</body>
</html>

4.模拟jQuery封装Ajax请求

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ajax实现前后台交互</title>
</head>
<body>
<form action="#" method="">
<input type="text" name="username" id="username" />
<input type="password" name="password" id="password" />
<input type="button" id="button" value="ajax登录" onclick="tzLogin();" />
</form> <div id="result"></div>
<script type="text/javascript">
var $ = {
ajax : function(opts){
//创建一个Ajax对象
var xhr = null; if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
var ids = ["MSXML2.XMLHTTP.3.0","Microsoft.XMLHTTP","MSXML2.XMLHTTP"];
for(var i=0;i<ids.length;i++){
try{
xhr = new ActiveXObject(ids[i]);
break;
}catch(e){ }
}
} xhr.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
var text = this.responseText;
if(opts.success) opts.success.call(this,text);
}else{
if(opts.error) opts.error.call(this,"调用有异常");
}
}; var params = "";
for(var k in opts.data){
if(params != "") {
params += "&";
}
params += (k+"="+opts.data[k]);
}
//true代表异步
if(opts.type.toLowerCase() == "get"){
var url = opts.url;
if(params != ""){
url = opts.url+"?"+params;
}
xhr.open("get",url,opts.async);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send();
}else if(opts.type.toLowerCase() == "post"){
xhr.open("post",opts.url,opts.async);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send(params);
}
} }; function tzLogin(){ $.ajax({
type: "post",
url: "login.jsp",
data: {
username:document.getElementById("username"),
password:document.getElementById("password")
},
dataType: "json",
async : true,
success: function(data){
document.getElementById("result").innerText = data;
},
error:function(){
document.getElementById("result").innerText = "调用有异常";
}
}); }
</script>
</body>
</html>

5.说明解析

ajax XMLHttpRequest
1.AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。
XML HttpRequest http请求的一种,传输的数据格式在早期常用XML

它是构建HTTP请求的javascript对象,在早期它是
ActiveX对象形式存在,服务器端和客户端传递异步的问题
早期数据的传递:字符串和XML
实际上,Ajax就是javascript和XML直接建立的一种异步传输的过程

2.创建 XMLHttpRequest 对象
创建 XMLHttpRequest 对象的语法:var xhr = new XMLHttpRequest();
老版本的 Internet Explorer (IE5 和 IE6)使用 ActiveX 对象:
var xhr = new ActiveXObject("Microsoft.XMLHTTP");

3.ajax 只不过是另一种Http请求的而已,它和form表单原理是一样的,
只不过不会刷新页面进行的一种异步交互

4.请求方式 get\post
get基于浏览器请求
1.直接在浏览器输入框输入一个地址进行请求
2.点击a链接是一个get请求
3.通过form指明method="get"
4.ajax指明请求方式get

post:form里面method="post"

5.请求的状态和响应异常
请求的状态,readyState
0:未初始化状态
1:载入请求的状态
2:载入完成状态
3:请求交互状态
4:请求完成状态s

可以捕获服务器错误
500服务器连接失败 -- 服务器关闭
404代表页面找不到
405请求方式有问题
200 代表交互成功,正确请求和响应
ajax里面指明post请求