基于http客户端的promise,面向浏览器和nodejsjavascript
浏览器端发起XMLHttpRequests请求vue
node端发起http请求java
支持Promise APInode
监听请求和返回ios
转化请求和返回npm
取消请求json
自动转化json数据axios
客户端支持抵御promise
使用npm:浏览器
npm install axios --save
为了解决post默认使用的是application/json请求数据 ,致使请求参数没法传递到后台,因此还须要安装一个插件QS,此插件将application/json转换为application/x-www-from-urlencoded
npm install qs --save
一个命令所有解决
npm install --save axios vue-axios qs
首先在 main.js 中引入 axios
import Axiso from 'axiso'
这时候若是在其它的组件中,是没法使用 axios 命令的。但若是将 axios 改写为 Vue 的原型属性,就能解决这个问题
Vue.prototype.$axios= Axios
配置好了以后就能够全局使用了
post请求转换
import QS from 'qs'
if(config.method=='post'){
config.data=QS.stringify(config.data);//防止post请求参数没法传到后台
}
axios({ method: 'post', url:'http://easy-mock.com/mock/596077559adc231f357bcdfb/axios/test-post-axios' }) .then((response)=>{ console.log(response.data) }) .catch((error)=>{ console.log(error) }) axios.post('http://easy-mock.com/mock/596077559adc231f357bcdfb/axios/test-post-axios',{ miaov:"课堂" //发送的数据 }) .then((response)=>{ console.log(response.data) }) .catch((error)=>{ console.log(error) })
发送带参数的
//get方式发送数据 axios.get('https://easy-mock.com/mock/5a883cccbf160328124e8204/example/mock', { params: { pomelo: 'tt', test: 'test' } }).then((response) => { console.log(response) }).catch((error) => { console.log(error) }) //post方式发送数据 axios.post('https://easy-mock.com/mock/5a883cccbf160328124e8204/example/mock', { pomelo: 'tt', test: 'test' }).then((response) => { console.log(response) }).catch((error) => { console.log(error) })