前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue使用axios解决跨域_vue前端解决跨域的方法

vue使用axios解决跨域_vue前端解决跨域的方法

作者头像
全栈程序员站长
发布2022-10-01 13:46:16
3.4K0
发布2022-10-01 13:46:16
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

工具版本: 【vue -V】:2.9.6 ide工具:VSCode / Idea

前提:我们前端vue工程需要单独部署

一、本地使用命令运行跨域问题。 外网访问的地址:https://www.runoob.com/try/ajax/json_demo.json 本地springboot接口访问的地址:http://192.168.3.12:8081/register/getSmsCode/123456789

1、axios访问的代码:

代码语言:javascript
复制
	created(){ 

const _this = this
this.$axios
.get('/try/ajax/json_demo.json')
.then(response => (
console.log('请求成功'),
console.log(response),
_this.msg = response.data
))
.catch(function (error) { 
 // 请求失败处理
console.log(error);
});
//发送本地springboot请求,本机的地址:192.168.3.12
this.$axios
.get('/register/getSmsCode/123456789')
.then(resp => (
console.log('请求本地接口OK'),
console.log(resp),
_this.springStr = resp.data
))
.catch(function (error) { 
 // 请求失败处理
console.log("请求本地接口失败");
});
}

截图如下:

在这里插入图片描述
在这里插入图片描述

备注:需要在main.js中全局注册

代码语言:javascript
复制
importaxios from 'axios'
Vue.prototype.$axios = axios    //全局注册,使用方法为:this.$axios

2、修改assetsPublicPath配置 配置 config—index.js中的build模块 将assetsPublicPath: ‘/’, 修改为 assetsPublicPath: ‘./’, 截图如下:

在这里插入图片描述
在这里插入图片描述

打包后的index.html路径为下面:

代码语言:javascript
复制
<script type=text/javascript src=./static/js/vendor.096d28cd4f5da166f9ce.js>

访问地址:http://localhost:8080/ 点击button跳转页面后的地址:http://localhost:8080/#/test

备注:这个/test是从首页的button跳转过来的 【this.$router.push(’/test’)】

3、修改proxyTable配置

代码语言:javascript
复制
		assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: { 

'/try': { 

target:'https://www.runoob.com', // 你请求的第三方接口
changeOrigin:true, // 在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
},
'/register': { 

target:'http://192.168.3.12:8081', // 本地的springboot接口
changeOrigin:true, //
}
},

截图如下:

在这里插入图片描述
在这里插入图片描述

4、测试运行: 我们通过【npm run dev】启动我们的服务,通过【http://localhost:8080】就可以访问。

代码语言:javascript
复制
我们本机可以访问,但是在其它的主机上访问拒绝(使用nginx部署不会有这个问题)
设置config---index.js中的
host: 'localhost', // can be overwritten by process.env.HOST
为:
host: '0.0.0.0', // can be overwritten by process.env.HOST

备注,使用proxyTable只能解决本地跨域问题。如果部署到nginx就不会

二、部署到nginx配置。(在window环境中) nginx版本:nginx version: nginx/1.19.0 启动nginx 直接在cmd中运行 nginx,不报错就启动了。 【tasklist|findstr “nginx”】可以查看是否启动过 结束服务【taskkill /f /im nginx.exe】

代码语言:javascript
复制
打包【npm run build】,生成dist文件夹,将dist里面的文件复制到nginx目录中【.../nginx/html/vue/】
配置nginx.conf文件
server {
listen       8888;
server_name  localhost;
#charset koi8-r;
#access_log  logs/host.access.log  main;
location / {
root   html;
index  index.html index.htm;
}
location /try {			
proxy_pass https://www.runoob.com;	//internet api
}
location /register {			
proxy_pass http://192.168.3.12:8081; // local spring boot api
}
访问地址:【http://localhost:8888/vue/#/】vue就是nginx里面创建的目录
点击button,可以正常axios请求(外网和本地的springboot接口)

本地的nginx配置如下图:

在这里插入图片描述
在这里插入图片描述

运行结果如下图:

在这里插入图片描述
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/194869.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月11日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档