欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php怎么返回数据给vue

程序员文章站 2022-03-16 13:31:36
...

php怎么返回数据给vue

php怎么返回数据给vue

1、首先vue发起网络请求可以使用axios库

推荐学习:Vue框架视频教程

1)安装axios

npm install axios --save

2)Vue使用axios

import 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的案例就完成了。

以上就是php怎么返回数据给vue的详细内容,更多请关注其它相关文章!

相关标签: php 返回数据