目录
推荐使用Axios更方便!
一、Fetch请求的使用
1、 Fetch官网
2、基本案例
二、跨域请求解决
(1)安装插件:
npm install --save-dev http-proxy-middleware@1.x
(2)在src目录下创建setupProxy.js文件,添加如下代码:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function (app) {
app.use(
createProxyMiddleware('/api', {
target: 'http://我是被代理的域名',
changeOrigin: true, //允许跨域
pathRewrite: {
'^/api': '' //路径重写
}
}),
);
};
请求时书写target对应路径的地方替换为'/api'即可。
如 http://localhost:8080/source/image.png
配置后应写为:/api/source/image.png 便可跨域请求。
三、Axios的使用
1、Axios官网
2、基本使用
首先需安装依赖:
npm install axios
或者直接引入cdn链接:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
并发请求在前面请求执行完后再执行后面的: