php怎么返回数据给vue
1、首先vue发起网络请求可以使用axios库
1)安装axiosnpm install axios --save
2)Vue使用axiosimport axios from "axios";
//将$axios挂在原型上,以便在实例中能用 this.$axios能够拿到
Vue.prototype.$axios = axios;
3)发起get请求this.$axios.get('/localhost/userinfo.php?userid=10001')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
2、php相应请求<?php
header('Content-Type:application/json; charset=utf-8');
$arr = array('userid'=>10001,'name'=>'老马','age'=>56);
exit(json_encode($arr));
vue手动php返回数据就会打印出来{userid: 10001, name: "老马", age: 56}
这样,php返回数据给vue的案例就完成了。