Home > Backend Development > PHP Problem > How to return data to vue in php

How to return data to vue in php

angryTom
Release: 2023-02-27 19:10:02
Original
3745 people have browsed it

How to return data to vue in php

How does php return data to vue

1. First, vue can use the axios library to initiate a network request.

Recommended learning: Vue framework video tutorial

1) Install axios

npm install axios --save
Copy after login

2) Vue uses axios

import axios from "axios";
//将$axios挂在原型上,以便在实例中能用 this.$axios能够拿到
Vue.prototype.$axios = axios;
Copy after login

3) Initiate a get request

this.$axios.get('/localhost/userinfo.php?userid=10001')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
Copy after login

2. PHP corresponding request

<?php
header(&#39;Content-Type:application/json; charset=utf-8&#39;);
$arr = array(&#39;userid&#39;=>10001,&#39;name&#39;=>&#39;老马&#39;,&#39;age&#39;=>56);
exit(json_encode($arr));
Copy after login

vue manual php return data will be printed out

{userid: 10001, name: "老马", age: 56}
Copy after login

In this way, the case of php returning data to vue is completed.

The above is the detailed content of How to return data to vue in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template