3 回答

TA貢獻1876條經(jīng)驗 獲得超7個贊
方法是利用vue-resource組件提供的一系列api:
get(url, [data], [success], [options])
post(url, [data], [success], [options])
put(url, [data], [success], [options])
patch(url, [data], [success], [options])
delete(url, [data], [success], [options])
jsonp(url, [data], [success], [options])
具體舉例如下:
1、導入vue-resource
<script src="js/vue.js"></script>
<script src="js/vue-resource.js"></script>
2、基于全局Vue對象使用http
// 通過someUrl獲取后臺數(shù)據(jù),成功后執(zhí)行then的代碼
Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
3、在一個Vue實例內(nèi)使用$http
// $http是在vue的局部范圍內(nèi)的實例
this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
說明:
在發(fā)送請求后,使用then方法來處理響應結果,then方法有兩個參數(shù),第一個參數(shù)是響應成功時的回調(diào)函數(shù),第二個參數(shù)是響應失敗時的回調(diào)函數(shù)。
添加回答
舉報