npm
npm install axios |
直接插入js
<script src="https://unpkg.com/axios/dist/axios.min.js"></script> |
或者下载到本地,随你喜欢
get请求
// 为给定 userid 创建请求 axios.get('/member.php?userid=100') .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
// 可选地,上面的请求可以这样做 axios.get('/member.php', { params: { userid: 100 } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); |
post请求
axios.post('/memberajaxurl', { username:username,action:"register_ajax" }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); |
axios({ method: 'post', url: '/memberajaxurl', data: { username:username,action:"register_ajax" } }).then(response=> { this.username_notice=response.data; }).catch(error=> { this.username_notice=error; }) |
*如果在Vue中使用无法将返回值指向this.data时,请使用箭头函数代替function。
axios.post('/memberajaxurl', { username:username,action:"register_ajax" }) .then( response=> { this.response=response; }) .catch(error=>{ this.response=error; }); |
下一篇:[vue]使用axios作http请求 post传参问题解决[真是一个坑]
上一篇:webpack-dev-server搭建一个便捷的前端开发环境