ajax请求插件vue-resource的学习

来源:互联网 发布:linux 查看cpu参数 编辑:程序博客网 时间:2024/06/11 04:38

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

<?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');
2 0
原创粉丝点击