〇、前情提要
在看b站视频 - 尚硅谷Web前端Ajax教程初学者零基础入门到精通全套完整版(ajax2020最新版),中插了这个知识记录一下。
参考:
- 我的笔记:
【javaweb】b站-尚硅谷Web前端Ajax教程初学者零基础入门到精通全套完整版(ajax2020最新版) 笔记
https://blog.csdn.net/weixin_43210113/article/details/110628389 - b站视频 - 尚硅谷Web前端Ajax教程初学者零基础入门到精通全套完整版(ajax2020最新版)
https://www.bilibili.com/video/BV1WC4y1b78y?p=2 - 笔记视频链接
链接:https://pan.baidu.com/s/1Wnwb42-AaqAlg6cQ0PRHwg
提取码:3waj - BootCDN
https://www.bootcdn.cn - MDN - WorkerOrGlobalScope.fetch()
https://developer.mozilla.org/zh-CN/docs/Web/API/WindowOrWorkerGlobalScope/fetch
————————————————————————————
- 我的笔记:
【ajax】9.Axios:Axios发送AJAX请求+Axios函数发送AJAX请求
https://blog.csdn.net/weixin_43210113/article/details/110823850
一、使用fetch函数发送AJAX请求
如没有安装过NodeJS和expressjs请看↓
我的笔记:
【ajax】NodeJS的安装与介绍+express框架介绍与基本使用
https://blog.csdn.net/weixin_43210113/article/details/110636188
如没有安装过nodemon请看
我的笔记:
【ajax】5.JSON:服务端响应JSON数据+nodemon自动重启工具安装+Missing write access to /usr/local/lib/node_modules解决方法
https://blog.csdn.net/weixin_43210113/article/details/110792442
WorkerOrGlobalScope.fetch()
参考:
MDN - WorkerOrGlobalScope.fetch()
https://developer.mozilla.org/zh-CN/docs/Web/API/WindowOrWorkerGlobalScope/fetch
Promise<Response> fetch(input[, init]);
fetch.html
btn.onclick = function(){
fetch('http://127.0.0.1:8000/fetch-server?vip=10', {
//请求方法
method: 'POST',
//请求头
headers: {
name:'atguigu'
},
//请求体
body: 'username=admin&password=admin'
}).then(response => {
console.log(response);
});
}
console.log(response);
返回一堆,其中有一个text()
btn.onclick = function(){
fetch('http://127.0.0.1:8000/fetch-server?vip=10', {
}).then(response => {
return response.text();
}).then(response=>{
console.log(response);
});
}
返回字符串
btn.onclick = function(){
fetch('http://127.0.0.1:8000/fetch-server?vip=10', {
}).then(response => {
return response.json();
}).then(response=>{
console.log(response);
});
}
返回json
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>fetch 发送 AJAX请求</title>
</head>
<body>
<button>AJAX请求</button>
<script>
//文档地址
//https://developer.mozilla.org/zh-CN/docs/Web/API/WindowOrWorkerGlobalScope/fetch
const btn = document.querySelector('button');
btn.onclick = function(){
fetch('http://127.0.0.1:8000/fetch-server?vip=10', {
//请求方法
method: 'POST',
//请求头
headers: {
name:'atguigu'
},
//请求体
body: 'username=admin&password=admin'
}).then(response => {
// return response.text();
return response.json();
}).then(response=>{
console.log(response);
});
}
</script>
</body>
</html>
server.js
//1. 引入express
const express = require('express');
//2. 创建应用对象
const app = express();
//3. 创建路由规则
//fetch 服务
app.all('/fetch-server', (request, response) => {
//设置响应头 设置允许跨域
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Headers', '*');
// response.send('Hello jQuery AJAX');
const data = {name:'尚硅谷'};
response.send(JSON.stringify(data));
});
//4. 监听端口启动服务
app.listen(8000, () => {
console.log("服务已经启动, 8000 端口监听中....");
});