【Vue】前端解决跨域问题——反向代理

在 axios 请求路径只需填写资源路径:

axios.get('/users').then(res => {
  console.log(res)
})

此时相当于向自己发送请求,会报 404。

然后在 vue.config,js 中配置反向代理:

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false, /*关闭语法检查*/
  // 配置反向代理
  devServer: {
    proxy: {
      // /adminapi/* - 后台管理使用
      // /webapi/* - 企业官网使用
      '/adminapi': {
        target: 'http://localhost:3000',
        changeOrigin: true,
      }
    }
  }
})

利用反向代理将请求转发出去。

在这里插入图片描述

请求成功!

你可能感兴趣的:(Vue,vue.js,前端,javascript)