vue中关于axios

一:.封装axios

  新建util文件夹  ----->  新建http.js文件

  

import axios from 'axios'

axios.defaults.withCredentials = true
const http = axios.create({
  baseURL: 'http://123.456.7.89:1011', //基地址
  timeout: 30000, //设置时间超时,单位毫秒
  withCredentials: true, //跨域请求时是否需要使用凭证
  crossDomain: true //跨域
})

export default https

使用时,在需要调用后台的地方

import http from '@/util/http'

在mounted或methods中

http.post('/后部分地址', 
{
headers: { Authorization: token },
xhrFields: {
withCredentials: true
}
}).then(res => {
 console.log(res)
})

------------------------------------------------------------------

http({
url: '',
methods: 'post',
params: {
},
headers: { Authorization: token }
}).then(res => {
console.log(res)
})

二:在main.js中

import axios from 'axios'

//配置请求的根路径 

axios.defaults.baseURL = 'http://123.456.7.89:1011' 

 Vue.prototype.$http = axios

使用的时候通过this.$http请求即可

三,data和params的区别

因为params是添加到url的请求字符串中的,用于get请求。 

而data是添加到请求体(body)中的, 用于post请求。
原文地址:https://www.cnblogs.com/brillant/p/15682443.html