Fork me on GitHub

vue解决跨域问题

1、什么是跨域

跨域问题的出现是因为浏览器的同源策略问题,同源是指 两个页面具有相同的 协议+主机+端口号,它是浏览器最核心也是最基本的功能。如果没有同源策略浏览器将会十分的不安全。

2、如何解决跨域问题

1)使用jsonp

2)使用jquery ajax

3)在vue开发中实现跨域,通过在devServer中配置 proxy

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  productionSourceMap: false, // 生产环境下开启 sourceMap,用于 fundebug 调试
  devServer: {
    open: true,
    host: 'localhost',
    port: 8080,
    proxy: {
      '/business': {
        target: 'http://36.***.***.***:8888',
        secure: false,
        ws: true,
        changeOrigin: true, //允许跨域
        logLevel: 'debug',
        pathRewrite: {
          // '^/business': ''
        }
      }
    }
  }
})

4)cors,后端配置 Access-Control-Allow-Origin 

 

posted @ 2023-12-27 17:00  锄田君  阅读(142)  评论(0)    收藏  举报