react 跨域请求

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、请求成功
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43287934/article/details/90484709