react 跨域请求

    xiaoxiao2022-07-13  153

    1、安装http-proxy-middleware

    npm install http-proxy-middleware

    2、在src目录下创建setupProxy.js,加入以下代码

    const proxy = require('http-proxy-middleware') module.exports = function (app) { app.use(proxy('/api', { target: 'http://localhost:8080', secure: false, changeOrigin: true, pathRewrite: { "^/api": "" } })) }

    3、在相应组件请求数据

    4、请求成功

    最新回复(0)