vue中get和post请求
vue中和后台交互,首先要引用vue-resource.js
vue-resource.js是专门和后台进行交互
<!-- ==============引入vue-resource插件=================-->
<script src="../js/vueJs/vue-resource.js"></script>
vue中get请求
function getRequest(url, params) {
return new Promise((resolve, reject) => {
Vue.$http.get(
url,
{
params: params
},
{emulateJSON: true}
)
.then((res) => { //成功的回调
resolve(res);
})
.catch((res) => { //失败的回调
reject(res);
});
});
}
vue的get请求传递参数的时候要用{params:{id:'1'}},这样来传递参数,否则就无法传递参数
vue中post请求
function postRequest(url, params) {
return new Promise((resolve, reject) => {
Vue.$http.post(
url,
{
params
},
{emulateJSON: true}
)
.then((res) => { //成功胡回调
resolve(res.body);
})
.catch((res) => { //失败的回掉
reject(res.body);
});
});
}
原文:https://blog.csdn.net/zhangjing1019/article/details/77946290