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 @   树山君  阅读(84)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示