
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>vue=resource基本使用</title>
<script src="./lib/jquery2.1.4.min.js"></script>
<script src="./lib/Vue2.5.17.js"></script>
<script src="https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.min.js"></script>
<link rel="stylesheet" href="./lib/bootstrap-3.3.7-dist/css/bootstrap.css">
<!-- Github : https://github.com/pagekit/vue-resource -->
<!-- 除了vue-resource之外,还可以使用 axios 的第三方包实现数据的请求 -->
<!-- 使用方式 this.$http.get || post || jsonp -->
</head>
<style>
</style> <body>
<div id="app">
<input type="button" class="btn btn-primary" value="get请求" @click="getInfo">
<input type="button" class="btn btn-primary" value="post请求" @click="postInfo">
<input type="button" class="btn btn-primary" value="jsonp请求" @click="jsonInfo">
</div> <script>
var vm = new Vue({
el: '#app',
data: {},
methods: {
getInfo() {//发起get请求
//当发起get请求之后,通过.then来设置,接收返回结果res是个对象。
//接口地址已失效
this.$http.get("http://vue.studyit.io/api/getlunbo").then(function(res){
console.log(res);
})
},
postInfo(){
//第一个传的对象是数据,第二个传的对象是配置选项
this.$http.post("http://vue.studyit.io/api/getlunbo",{},{emulateJSON:true}).then(res => {
console.log(res);
})
},
jsonInfo(){//发起jsonp请求
this.$http.post("http://vue.studyit.io/api/jsonp").then(res => {
console.log(res);
})
}
},
})
</script>
</body> </html>