第七篇 - axios跨域问题

SpringBoot和Vue项目可以连接起来,当登录成功,要跳转到下一个页面时,在跨域请求,如果后端需要携带cookies的话,前端就必须设置axios.defaults.withCredentials=true,不然API会访问不成功。

参考链接:https://blog.csdn.net/Girl_0919/article/details/109075719

设置方法:

main.js

复制代码
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
import './assets/css/global.css'
import axios from 'axios'

Vue.use(ElementUI)
Vue.config.productionTip = false
Vue.prototype.$axios = axios
axios.defaults.withCredentials = true

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
复制代码

 

需要添加这一行,才能成功的跨域访问。

 
posted @   o云淡风轻o  阅读(73)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2019-07-04 第十七篇 -- 学习第十六天打卡20190704
点击右上角即可分享
微信分享提示