單來講,vue-resource就像jQuery裏的$.ajax,用來和後端交互數據的。能夠放在created或者ready裏面運行來獲取或者更新數據... ###vue-resource特色 vue-resource插件具備如下特色:html
1.引入vue-resourcevue
####基本語法ios
// 基於全局Vue對象使用http Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback); Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback); // 在一個Vue實例內使用$http this.$http.get('/someUrl', [options]).then(successCallback, errorCallback); this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);git
在發送請求後,使用then方法來處理響應結果,then方法有兩個參數,第一個參數是響應成功時的回調函數,第二個參數是響應失敗時的回調函數。github
JSONP
的方式與get請求相同const url = 'http://vue.studyit.io/api/getnewslist'
this.$http.get(url)
.then(data => {
console.log(data)
console.log(data.body)
})
複製代碼
// const url = 'http://182.254.146.100:8899/api/postcomment/17'
const url = 'http://vue.studyit.io/api/postcomment/17'
this.$http.post(url, {
content: '完美!'
}, {
emulateJSON: true
})
.then(data => {
console.log(data.body);
})
複製代碼
const url = 'http://v.showji.com/Locating/showji.com2016234999234.aspx?m=13333333333&output=json&×tamp=' + (new Date() - 0)
this.$http.jsonp(url)
.then(data => {
console.log(data.body);
})
複製代碼