vue-resource
<script src="./lib/vue-2.4.0.js"></script>
<!-- 注意:vue-resource 依賴于 Vue,所以先后順序要注意 -->
<!-- this.$http.jsonp -->
<script src="./lib/vue-resource-1.3.4.js"></script>
<div id="app">
<input type="button" value="get請求" @click="getInfo">
<input type="button" value="post請求" @click="postInfo">
<input type="button" value="jsonp請求" @click="jsonpInfo">
</div>
// 創(chuàng)建 Vue 實例,得到 ViewModel
var vm = new Vue({
el: '#app',
data: {},
methods: {
getInfo() { // 發(fā)起get請求
// 當發(fā)起get請求之后, 通過 .then 來設置成功的回調(diào)函數(shù)
this.$http.get('http://vue.studyit.io/api/getlunbo').then(function (result) {
// 通過 result.body 拿到服務器返回的成功的數(shù)據(jù)
// console.log(result.body)
})
},
postInfo() { // 發(fā)起 post 請求 application/x-wwww-form-urlencoded
// 手動發(fā)起的 Post 請求,默認沒有表單格式,所以,有的服務器處理不了
// 通過 post 方法的第三個參數(shù), { emulateJSON: true } 設置 提交的內(nèi)容類型 為 普通表單數(shù)據(jù)格式
this.$http.post('http://vue.studyit.io/api/post', {}, { emulateJSON: true }).then(result => {
console.log(result.body)
})
},
jsonpInfo() { // 發(fā)起JSONP 請求
this.$http.jsonp('http://vue.studyit.io/api/jsonp').then(result => {
console.log(result.body)
})
}
}
});