<body>
<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>
<script>
var vm=new Vue({
el:'#app',
data:{},
methods:{
getInfo(){ //get请求,通过function执行的成功的回调函数,得到body和data等数据
this.$http.get('http://jsonplaceholder.typicode.com/users').then(function(result){
console.log(result.body);
// 通过result.body 获得服务器返回的数据
})
},
//post请求,中间花括号空的部分为提交给服务器的数据这里默认,emulateJSON:true,将手动提交表单格式设置为application/x-www-form-urlencoded格式
postInfo(){
this.$http.post('http://jsonplaceholder.typicode.com/users',{},{emulateJSON:true}).then(result =>{
console.log(result.body);
// 通过result.body 获得服务器返回的数据
})
},
jsonpInfo(){
this.$http.post('http://jsonplaceholder.typicode.com/users').then(result =>{
console.log(result.body);
// 通过result.body 获得服务器返回的数据
})
}
}
});
</script>
</body>
vue学习十六 --- vue-resource三种请求方式
最新推荐文章于 2024-05-11 14:50:41 发布