vue 2.x axios 封装的get 和post方法
import axios from 'axios' import qs from 'qs' export class HttpService { Get(url, data) { return new Promise((resolve, reject) => { axios.get(url, { params: data }).then((res) => { if (res) { //成功回调 resolve(res); } }).catch((error) => { reject(error); }) }) } Post(url, data) { return new Promise((resolve, reject) => { axios.post(url, qs.stringify(data), { headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' } }).then((res) => { if (res) { //成功回调 resolve(res); } }).catch((error) => { reject(error); }) }) } }
postfile方法
PostFlie(url, data) { return new Promise((resolve, reject) => { //根据data对象生成FormData对象 var temp = new FormData(); for (var t in data) { temp.append(t, data[t]); } axios.post(url, temp).then((res) => { if (res) { resolve(res.Data); } }).catch((error) => { reject(error); }) }) }
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
以上就是在vue 2.x 中使用axios如何封装的get 和post方法的详细内容,更多请关注php中文网其它相关文章!
![php中文网最新课程二维码](/static/images/article_wechat2021.jpg?1111)
声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论