vue 解决跨域问题(开发环境)

一、什么是跨域问题

同源:域名,协议,端口均相同

不同源就是跨域,比如你的前端为localhost:9528,后端为localhost:8080,此时前端去访问后端接口就会产生跨域问题,因为端口不同。

这里不详细讲,可参考百度百科-同源策略

二、如何解决

跨域问题前后端都可以解决,这里仅提供vue的前端跨域解决方案:

前端vue.config.js配置如下,然后保证在你访问后端接口时加个前缀/api

devServer: {
    port: port,
    open: false,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: { // vue2.x版本为 proxyTable,vue3.x为proxy。
      // detail: https://cli.vuejs.org/config/#devserver-proxy
      '^/api': {
        target: 'http://localhost:9999', // 这里的端口是后端端口
        changeOrigin: true,
        secure: false,  // 如果是https接口,需要配置这个参数为true
        pathRewrite: {  // 路径重写
          '^/api':''
        }
      }
    }
  }

三、例子

以下例子访问接口的时候都会保证访问的路径为:/api/about/first,包含前缀/api

例1 以axios为例

axios.get('/api/about/first', function (res) { 
   console.log(res) 
})

例2 以 vue-element-admin为例

request.js:baseURL这里不写 baseURL

const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 10000 // request timeout
})

api/xxx.js: 在接口这里加前缀

import request from '@/utils/request.js'

export default {
  getFirst() {
    return request({
      url: '/api/about/first',
      method: 'get'
    })
  }
  }

vue.config.js:

devServer: {
    port: port,
    open: false,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      // change xxx-api/login => mock/login
      // detail: https://cli.vuejs.org/config/#devserver-proxy
      '^/api': {
        target: 'http://localhost:9999', // 这里的端口是后端端口
        changeOrigin: true,
        secure: false,  // 如果是https接口,需要配置这个参数为true
        pathRewrite: {
          '^/api':''
        }
      }
    }
  }

例3 仍以 vue-element-admin为例

request.js:baseURL这里加前缀

const service = axios.create({
  baseURL: '/api', // url = base url + request url process.env.VUE_APP_BASE_API+'api'
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

api/xxx.js:这里不加前缀

import request from '@/utils/request.js'

export default {
  getFirst() {
    return request({
      url: '/about/first',
      method: 'get'
    })
  }
  }

vue.config.js:

devServer: {
    port: port,
    open: false,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      // change xxx-api/login => mock/login
      // detail: https://cli.vuejs.org/config/#devserver-proxy
      '^/api': {
        target: 'http://localhost:9999', // 这里的端口是后端端口
        changeOrigin: true,
        secure: false,  // 如果是https接口,需要配置这个参数为true
        pathRewrite: {
          '^/api':''
        }
      }
    }
  }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值