ajax请求插件vue-resource的学习

http://cn.vuejs.org/guide/plugins.html

ajax请求插件vue-resource的学习

https://github.com/vuejs/vue-resource/blob/master/README.md

1、安装

npm install vue-resource

2、使用

import VueResource from 'vue-resource';
Vue.use(VueResource);
this.$http.get("http://localhost/test.php").then(
            function (res) {
                // 处理成功的结果
                alert(res.body);
            },function (res) {
            // 处理失败的结果
            }
        );

传递数据到后端

this.$http.post("http://localhost/test.php",{name:"zhangsan"},{emulateJSON:true}).then(
            function (res) {
                // 处理成功的结果
                alert(res.body);
            },function (res) {
            // 处理失败的结果
            }
        );

3、test.php

// 指定允许其他域名访问
header('Access-Control-Allow-Origin:*');

// 响应类型
header('Access-Control-Allow-Methods:GET,POST,PUT');
header('Access-Control-Allow-Headers:x-requested-with,content-type');


var_export($_POST);

die('hello');

你可能感兴趣的:(ajax请求插件vue-resource的学习)