Vue3:解决基地址不同 数据交互http与https跨域问题

编程入门 行业动态 更新时间:2024-10-26 01:28:17

Vue3:解决<a href=https://www.elefans.com/category/jswz/34/1768839.html style=基地址不同 数据交互http与https跨域问题"/>

Vue3:解决基地址不同 数据交互http与https跨域问题

配置公共管理的api文件和vue.config.js可以解决跨域问题。一个项目对接不同的基地址和接口同理。

api

export default {//接口基地址Millia: process.env.NODE_ENV == 'development' ? location.protocol + '//' + location.host + '/milliaApi' : '/index.php/',MilliaStudy:process.env.NODE_ENV=='development'?location.protocol+'//'+location.host+'/MilliaStudyApi':'/xxx',//不同接口GETREC:'api/XXXXX',GETCATEGORY:'api/XXXX',
}

vue.config.js

//devServer 是一个本地开发服务器,会自动监听变化,自动打包构建,自动更新刷新浏览器devServer: {hot: true, //热加载host: 'localhost',port: 8080, //端口https: false, //false关闭https,true为开启open: true, //自动打开浏览器proxy: {'/milliaApi': {target: '/',ws: true,changeOrigin: true,pathRewrite: {'^/milliaApi': '/'}},'/MilliaStudyApi': {target: '/xxx',ws: true,changeOrigin: true,pathRewrite: {'^/MilliaStudyApi': '/'}},}},

基地址对应不同的http协议(http和https),线上项目会报跨域

方法一:

去除api和vue.config.js里 http: 和 https: 不会报跨域但是有

方法二:

不修改api和vue.config.js,在index.html的head中中添加

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

更多推荐

Vue3:解决基地址不同 数据交互http与https跨域问题

本文发布于:2023-11-16 10:21:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:基地   数据   http   https

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!