单来讲,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);
})
复制代码