vue-resource 实现 get,post,jsonp 请求
- 除了 vue-resource 之外,还可以使用
axios
的第三方包实现数据的请求<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> var vm = new Vue({ el: '#app', data: { }, methods: { getInfo() { // 发起 get 请求 // 当发起 get 请求之后,通过 .then 来设置成功的回调函数 this.$http.get('请求地址').then(function(result) { // 成功时的回调,必须写 // 通过 result.body 拿到服务器返回的成功的数据 console.log(result.body); }); }, postInfo() { // 发起 post 请求 application/x-www-form-urlencoded // 手动发起的 post 请求,默认没有表单格式,所以,有的服务器处理不了 // 通过 post 方法的第三个参数,{ emulateJSON: true } 设置提交的内容类型为普通表单数据格式 this.$http.post('请求地址',{},{ emulateJSON: true }).then(result=>{ console.log(result.body); }); }, jsonpInfo() { // 发起 jsonp 请求 this.$http.jsonp('请求地址').then(result=>{ console.log(result.body); }); } }, });