一、express基本使用
1. 在最外层启动终端,添加文件
![](https://i-blog.csdnimg.cn/direct/28143563723f4805bd45a9b740ea8ada.png)
![](https://i-blog.csdnimg.cn/direct/a091c45d335b42a3973584fd11659451.png)
2. 创建 express 框架
![](https://i-blog.csdnimg.cn/direct/f7730c6d86f442a0a8b807d547b2a036.png)
javascript
// 1. 引入express
const express = require('express');
// 2. 创建应用对象
const app = express();
// 3. 创建路由规则
//request 是对请求报文的封装
//response 是对响应报文的封装
app.get('/' , (express,request)=>{
// 设置响应
response.send('HELLO EXPRESS');
});
// 4. 监听端口启动服务
app.listen(8000, ()=>{
console.log("服务已启动,8000 端口监听中....");
});
3. 右击这里点开在终端打开
![](https://i-blog.csdnimg.cn/direct/baf40f6a2a0a47ba80410b8515d1a72f.png)
4. 输入node express基本使用·js
![](https://i-blog.csdnimg.cn/direct/f4302cdc9f8548c2bb1e1d33f29419a3.png)
5. 打开浏览器 搜索网址127.0.0.1:8000
![](https://i-blog.csdnimg.cn/direct/62bdd2f2f80d4631aa6c33358d65dc74.png)
注意:这三个文件都要有,一个没有都不可以
二、原生AJAX
1. 前端准备
![](https://i-blog.csdnimg.cn/direct/5b7bb0e54be64e9793bd43b740950f31.png)
2.服务端准备 (express框架)
![](https://i-blog.csdnimg.cn/direct/226c77b7aa5843e596c0b26d14cc1c15.png)
tip :8000端口已经被占用
在node中按Ctrl C 关闭八千端
回到原来终端,在用node 文件名,实现运行
tip :看到 xhr 一般要想到ajax
3. get
响应头只要是2开头的都表示成功
![](https://i-blog.csdnimg.cn/direct/1e3a6c60e0404cc4820a56b9b9cb8c28.png)
![](https://i-blog.csdnimg.cn/direct/c4f2434c487d43dfba9a598a5a99e724.png)
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AJAX GET 请求</title>
<style>
#result{
width:200px;
height:100px;
border:solid 1px #90b;
}
</style>
</head>
<body>
<button>点击发送请求</button>
<div id="result"></div>
<script>
//获取button元素
const btn = document.getElementsByTagName('button')[0];
const result = document.getElementById("result");
//绑定事件
btn.onclick = function(){
//1. 创建对象
const xhr = new XMLHttpRequest();
//2. 初始化 设置请求方法和 url
xhr.open('GET', 'http://127.0.0.1:8000/server?a=100&b=200&c=300');
//3. 发送
xhr.send();
//4. 事件绑定 处理服务端返回的结果
// on when 当....时候
// readystate 是 xhr 对象中的属性, 表示状态 0 1 2 3 4
// change 改变
xhr.onreadystatechange = function(){
//判断 (服务端返回了所有的结果)
if(xhr.readyState === 4){
//判断响应状态码 200 404 403 401 500
// 2xx 成功
if(xhr.status >= 200 && xhr.status < 300){
//处理结果 行 头 空行 体
//响应
// console.log(xhr.status);//状态码
// console.log(xhr.statusText);//状态字符串
// console.log(xhr.getAllResponseHeaders());//所有响应头
// console.log(xhr.response);//响应体
//设置 result 的文本
result.innerHTML = xhr.response;
}else{
}
}
}
}
</script>
</body>
</html>
4.get请求中设置参数
在 url 后面缀参数,用 问号 分隔,多个参数之间用 & 分隔
![](https://i-blog.csdnimg.cn/direct/7aa0dba64d144dba92b9ce7ae928adb8.png)
5.post
js代码修改之后,需要重新调用后台,Ctrl C关闭之前端口,重新启用新端口
![](https://i-blog.csdnimg.cn/direct/49ed078113c74410907d39165fe0ab17.png)
![](https://i-blog.csdnimg.cn/direct/6aabc43c1d014efd8262f6e733123ada.png)
6. post请求中设置参数
![](https://i-blog.csdnimg.cn/direct/30eaf2438d874e9f913ae91b255655cf.png)
7. 设置请求头
![](https://i-blog.csdnimg.cn/direct/257fb5c962b0409fab8067a43ab9d08d.png)
8.JSON
注意:send( )只能接受字符串类型,想要传递对象等其他类型,则必须进行转换
![](https://i-blog.csdnimg.cn/direct/a17ac44029e24e8d8d7bb2eb4a264937.png)
黄框中的响应头,可以设置任意的响应头,自定义的也可以
红框中,将对象转变成字符串,使得send可以传递对象
获取复杂的字符串数据
![](https://i-blog.csdnimg.cn/direct/190622f764094e3996a766c39af720ca.png)
9. IE缓存
针对 IE 缓存的 JS 代码
HTML代码
10. 请求超时和网络异常处理
![](https://i-blog.csdnimg.cn/direct/bc57caefb66049f682c67c03f8fe4e83.jpeg)
timeout 超时取消(超时的话,取消该操作)
![](https://i-blog.csdnimg.cn/direct/67790871f68d420f8507c34074ee818d.png)
11. 取消请求
用 abort
![](https://i-blog.csdnimg.cn/direct/28a59f8b065b4b6980ebebcc984c0985.png)
12. 重复发送请求
一旦结果是false,就不会发送新的请求
![](https://i-blog.csdnimg.cn/direct/f782c53d2e194ac4b2c37f8ae02412c6.png)
三、jQuery 中的AJAX
1. get 与 post
![](https://i-blog.csdnimg.cn/direct/32adc2faab4c4b08958a783bbb007b58.png)
加 json 会变为对象,不加的话处理完会变成字符串
html
$('button').eq(0).click(function(){
$.get('http://127.0.0.1:8000/jquery-server', {a:100, b:200}, function(data){
console.log(data);
},'json');
});
$('button').eq(1).click(function(){
$.post('http://127.0.0.1:8000/jquery-server', {a:100, b:200}, function(data){
console.log(data);
});
});
2. 通用方法
将响应体从字符串变为对象
html
<script>
$('button').eq(0).click(function(){
$.get('http://127.0.0.1:8000/jquery-server', {a:100, b:200}, function(data){
console.log(data);
},'json');
});
$('button').eq(1).click(function(){
$.post('http://127.0.0.1:8000/jquery-server', {a:100, b:200}, function(data){
console.log(data);
});
});
$('button').eq(2).click(function(){
$.ajax({
//url
url: 'http://127.0.0.1:8000/jquery-server',
//参数
data: {a:100, b:200},
//请求类型
type: 'GET',
//响应体结果
dataType: 'json',
//成功的回调
success: function(data){
console.log(data);
},
//超时时间
timeout: 2000,
//失败的回调
error: function(){
console.log('出错啦!!');
},
//头信息
headers: {
c:300,
d:400
}
});
});
</script>
四、axios
1. 发送AJAX请求
① get
![](https://i-blog.csdnimg.cn/direct/9d54a0acfea444429fb0726974aaef64.png)
② post
- 先url 2. 再请求体 3. 再其他配置
![](https://i-blog.csdnimg.cn/direct/055210373a7c4ac189179ca370cb2d72.png)
③ 通用方式
![](https://i-blog.csdnimg.cn/direct/352ccf298c6044bb85d8bae6acd75926.png)
html
btns[2].onclick = function(){
axios({
//请求方法
method : 'POST',
//url
url: '/axios-server',
//url参数
params: {
vip:10,
level:30
},
//头信息
headers: {
a:100,
b:200
},
//请求体参数
data: {
username: 'admin',
password: 'admin'
}
}).then(response=>{
//响应状态码
console.log(response.status);
//响应状态字符串
console.log(response.statusText);
//响应头信息
console.log(response.headers);
//响应体
console.log(response.data);
})
}
五、fetch发送AJAX请求
![](https://i-blog.csdnimg.cn/direct/3e5245d84bfb41dcb3c2b8d529459bcb.png)
想要单独只获取到响应体结果,用如下代码
六、跨域
1. 同源策略
![](https://i-blog.csdnimg.cn/direct/4d63bb25c79140c1a0b1bc326db3e26b.png)
【案例】
![](https://i-blog.csdnimg.cn/direct/dce5b10dba324ebb9de8c9caa43d3bd7.png)
2. JSONP
① 【案例】
JS
![](https://i-blog.csdnimg.cn/direct/0af293de0bb1484d94b6917b02caa037.png)
HTML
② 发送请求
为了防止报错,可以加一个这个
注意一定要加参数 callback = ?
JS
![](https://i-blog.csdnimg.cn/direct/ef26e81a6ec5449e82b8c325e90f8c0f.png)
HTML
3. CORS
![](https://i-blog.csdnimg.cn/direct/795b5725827e4e759cb0e57b8d5c6850.png)
在服务端接口设置一个响应头就可以实现跨域
后面用 * 对所有的网页都好使(通配)
![](https://i-blog.csdnimg.cn/direct/2ff92c53be97426e82a8d9ce9c219ee6.png)
好啦~本次的分享到这里就结束啦~~~我们下次不见不散~~~