axios+跨域+携带cookie
2019-05-19 本文已影响0人
简小园
axios的使用
- cmd下载
npm i axios vue-axios
- 在main.js中引入
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios);
跨域设置
- 在config目录下的index.js的dev下配置
proxyTable: { //设置地址代理,跨域请求外部链接
'/api': {
target: 'http://00.00.00.00',//设置你调用的接口域名和端口号 别忘了加http
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
},

- 发送请求是加
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
this.axios({
method: 'get/post',
url: '',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {}
}).then(function (res) {
console.log(res.data)
}).catch(function (error) {
console.log(error)
});
携带cookie
在main.js中加axios.defaults.withCredentials=true;
见下图:
