Vue-resource实现ajax请求和跨域请求

vue-resource是Vue提供的体格http请求插件,如同jQuery里的$.ajax,用来和后端交互数据的。

在使用时,首先须要安装vue-resource插件php

1.在项目跟目录上安装:html

npm install vue-resource

2.引入resource插件vue

import VueResource from 'vue-resource';  
Vue.use(VueResource)

3.发送请求:ajax

this.$http.get("http://www.vrserver.applinzi.com/aixianfeng/apihome.php").then(function(res){  
        console.log(res)  
    })

ES6写法:npm

this.$http.get('url', [options]).then((res) => {  
</span><pre name="code" class="html" style="color: rgb(68, 68, 68);"><span style="font-family:Microsoft YaHei;font-size:18px;">// 处理成功的结果</span></pre>}, (res) => { // 处理失败的结果});

在发送请求后,使用then方法来处理响应结果,then方法有两个参数,第一个参数是响应成功时的回调函数,第二个参数是响应失败时的回调函数。后端

then方法的回调函数也有两种写法,第一种是传统的函数写法,第二种是更为简洁的ES 6的Lambda写法:api

POST请求:app

this.$http.post("http://www.vrserver.applinzi.com/aixianfeng/apihome.php",{name:"abc"},{emulateJSON:true}).then(  
            function (res) {  
                // 处理成功的结果  
                alert(res.body);  
            },function (res) {  
            // 处理失败的结果  
            }  
        );
相关文章
相关标签/搜索