步骤
ajax:Web数据交互方式
- 用于:局部刷新页面。
- 使用场景:
- 1.搜索框实时显示结果
- 2.动态加载页面
- 3.列表页面的分页
- 4.表单的服务器端校验
ajax优点:
- 不需要任何插件的支持,利用js就可以使用。
- 提高用户的体验。
- 提高web程序的性能。
- 减轻服务器端的宽带压力。
使用步骤:
- 1.常见xmlhttprequest对象
- 2.调用对象的open方法
- 3.调用对象的send方法
- 4.监听状态改变 onreadystatechange
- 4.1判断对象的readStatus是否等于4
- 4.2 判断响应的状态码是否等于200
- 4.3 对响应的结果进行处理
get请求和post请求的区别:
- 1.get请求的参数是拼接在地址栏中。 post请求的参数是拼接在send函数中。、
- 2.post请求需要设置请求头。
- xhr.setRequestHeader(“content-Type”,“application/x-www-form-urlencoded”);
get的方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" id="bookId">
<button>查询</button>
<div id="result">
<ul>
<li>书名:<span id="bookname"></span></li>
<li>作者:<span id="author"></span></li>
<li>封面:<img src="" alt="" id="imgSrc"></li>
</ul>
</div>
<script>
let btn = document.querySelector("button");
let rst = document.querySelector("#result");
let bookId = document.querySelector("#bookId")
let bookName = document.querySelector("#bookname");
let author = document.querySelector("#author");
let imgSrc = document.querySelector("#imgSrc");
btn.onclick = sendMsg;
function sendMsg(){
let xhr = new XMLHttpRequest();
xhr.open("get","http://localhost:3000/book/select?id="+bookId.value);
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if (xhr.status === 200) {
let result = JSON.parse(xhr.responseText)[0];
bookName.innerHTML = result.bookName;
author.innerHTML = result.author;
imgSrc.src = result.imgSrc;
}
}
}
}
</script>
</body>
</html>
post方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" id="bookId">
<button>查询</button>
<div id="result">
<ul>
<li>书名:<span id="bookname"></span></li>
<li>作者:<span id="author"></span></li>
<li>封面:<img src="" alt="" id="imgSrc"></li>
</ul>
</div>
<script>
let btn = document.querySelector("button");
let rst = document.querySelector("#result");
let bookId = document.querySelector("#bookId")
let bookName = document.querySelector("#bookname");
let author = document.querySelector("#author");
let imgSrc = document.querySelector("#imgSrc");
btn.onclick = sendMsg;
function sendMsg(){
let xhr = new XMLHttpRequest();
xhr.open("post","http://localhost:3000/book/select");
xhr.setRequestHeader("content-Type","application/x-www-form-urlencoded");
xhr.send('id='+bookId.value);
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if (xhr.status === 200) {
let result = JSON.parse(xhr.responseText)[0];
bookName.innerHTML = result.bookName;
author.innerHTML = result.author;
imgSrc.src = result.imgSrc;
}
}
}
}
</script>
</body>
</html>
jQuery是写的Ajax方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" id="bookId">
<button id="btn">查询</button>
<div id="result">
<ul>
<li>书名:<span id="bookname"></span></li>
<li>作者:<span id="author"></span></li>
<li>封面:<img src="" alt="" id="imgSrc"></li>
</ul>
</div>
<script src="js/jquery-3.6.0.js"></script>
<script>
$("#btn").on("click", function () {
$.ajax({
type: "post",
url: "http://localhost:3000/book/select",
data:{
id:$("#bookId").val()
},
dataType: "json",
success: function (data) {
$("#bookname").html(data[0].bookName);
$("#author").html(data[0].author);
$("#imgSrc").attr("src",data[0].imgSrc);
},
error: function (err) {
console.log(err);
}
});
})
</script>
</body>
</html>