第七篇 - 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 @ 2023-07-04 11:16  o云淡风轻o  阅读(58)  评论(0编辑  收藏  举报