Ajax底层的基本实现
文章目录
基本步骤
- 创建XHR对象
- 设置状态监听
- 建立与服务器端的连接
- 发送请求
最初代码
function doAjaxGet(){
//1,创建XHR对象(此对象是Ajax技术应用的入口对象,发送异步请求,处理响应结果都是通过此对象实现)
var xhr = new XMLHttpRequest();
//2,设置状态监听
xhr.onreadystatechange=function(){//事件监听函数(处理客户端与服务器通过过程中产生的数据)
/**readyState的值0,1,2,3,4
* 0.表示还未执行open方法
* 1.表示执行open方法但还未执行send方法
* 2.表示已执行send方法
* 3.表示客户端正在接收服务端响应的数据
* 4.表示客户端已经完成响应数据的接收
*/
if(xhr.readyState==4&&xhr.status==200){
var result = xhr.responseText;//获取服务器端响应文本数据
document.getElementById("resultId").innerHTML=result;
}
}
//3,建立与服务器端的连接
xhr.open("GET","doAjaxGet",true);//true表示异步请求,false表示同步,请求会在main线程发送
//4,发送请求
xhr.send(null);//将请求交给ajax引擎
console.log("do other .....");
}提取代码共同点进行初步封装
function doAjaxGet(url,params,callback){
let xhr = new XMLHttpRequest();
xhr.onreadystatechange=function (){
if(xhr.readyState==4&&xhr.status==200){
callback(xhr.responseText);
}
}
xhr.open("GET",`${url}?${params}`,true);
xhr.send(null);
}
function doCheck(){//发送异步请求,检查name是否存在
let url = `http://localhost:8080/doCheck`;
let name = document.forms[0].name.value;
doAjaxGet(url,`name=${name}`,function (result){
document.getElementById("result").innerHTML=result;
})
}
function doAjaxPost(url,params,callback){
let xhr = new XMLHttpRequest();
xhr.onreadystatechange=function (){
if(xhr.readyState==4&&xhr.status==200){
callback(xhr.responseText);
}
}
xhr.open("POST",`${url}?${params}`,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(`name=${name}`);
}
function doSave(){
let name = document.forms[0].name.value;//获取表单中name对应的value属性值
let url = "/doSave";
let params = `name=${name}`;
doAjaxPost(url,params,function (result){
document.getElementById("result").innerHTML=result;
})
}封装成Ajax对象写入js文件
Ajaxfk.js文件代码
(function(){
var ajax = function (){};
ajax.prototype={
doAjaxGet:function(url,params,callback){
let xhr = new XMLHttpRequest();
xhr.onreadystatechange=function (){
if(xhr.readyState==4&&xhr.status==200){
callback(xhr.responseText);
}
}
xhr.open("GET",`${url}?${params}`,true);
xhr.send(null);
},
doAjaxPost:function(url,params,callback){
let xhr = new XMLHttpRequest();
xhr.onreadystatechange=function (){
if(xhr.readyState==4&&xhr.status==200){
callback(xhr.responseText);
}
}
xhr.open("POST",`${url}?${params}`,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(`name=${name}`);
}
}
window.Ajax = new ajax();
})()引入js文件
<script src="js/ajaxfk.js"></script><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/ajaxfk.js"></script>
</head>
<body>
<h1>The Ajax 02 Page</h1>
<fieldset>
<legend>ajax 表单请求</legend>
<form><input type="text" name="name" onblur="doCheck()" id="nameId"/>
<button type="button" onclick="doSave()">save</button>
</form> <span id="result"></span>
</fieldset>
<script>
function doSave(){
let name = document.forms[0].name.value;//获取表单中name对应的value属性值
let url = "/doSave";
let params = `name=${name}`;
Ajax.doAjaxPost(url,params,function (result){
document.getElementById("result").innerHTML=result;
})
}
function doCheck(){//发送异步请求,检查name是否存在
let url = `http://localhost:8080/doCheck`;
let name = document.forms[0].name.value;
Ajax.doAjaxGet(url,`name=${name}`,function (result){
document.getElementById("result").innerHTML=result;
})
}
</script>
</body>
</html>添加jquery.min.js
function doAjaxPost(){
let url = "http://localhost:8080/doPost";
let params = "id=1&name=huawei&letter=x";
$.post(url,params,function (result){
console.log(result);
})
}
function doAjaxGet(){
let url = "http://localhost:8080/doGet";
let params = "";
$.get(url,params,function (result){
console.log(result);
});
}
添加axios.min.js
function doAxiosAjaxGet(){
let url = "http://localhost:8080/doGet";
axios.get(url).then(function (result){
console.log(result)
document.querySelector(".result").innerHTML=JSON.stringify(result);
})
}
function doAxiosAjaxPost(){
let url = "http://localhost:8080/doPostJson";
let params = {"msg":"hello axios"};
axios.post(url,params).then(function (result){
console.log(result)
document.querySelector(".result").innerHTML=JSON.stringify(result);
}).catch(function (error){
console.log(error);
})
}controller方法
@RestController
public class JqueryController {
@RequestMapping("/doPostJson")
public String doPostJson(@RequestBody Map<String,Object> map){
System.out.println("mpa="+map);
return "doPost ok";
}
@RequestMapping("/doPost")
public String doPost(@RequestParam Map<String,Object> map){
System.out.println("mpa="+map);
return "doPost ok";
}
@RequestMapping("/doAjax")
public String doAjax(String msg){
System.out.println("msg="+msg);
return msg+".toUpperCase"+msg.toUpperCase();
}
@RequestMapping("/doGet")
public Map<String,Object> doAjaxGet(){
Map<String,Object> map = new HashMap<>();
map.put("code",1);
map.put("message","success");
return map;
}
}学习总结,仅供复习用
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论