ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
axios请求PHP发过来的JSON方法 # 方法1: ~~~ getDate:function (){ var that=this; //window转换为vue axios.get('http://127.0.0.1/index.php').then(function (res){ that.user=res.data.name; //name为PHP端的JSON键 user为要输出的VUE里自定义的变量 console.log(that.user) //打印输出到控制台把数值 }) ~~~ ## 代码案例: ~~~ <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <title>Document</title> </head> <body> <div id="app"> <button type="button" @click="getDate">获取数据</button> <p>{{user}}</p> </div> </body> </html> <script type="text/javascript"> var vm= new Vue({ el:'#app', data:{ user:'1223' }, methods:{ getDate:function (){ var that=this; //window转换为vue axios.get('http://127.0.0.1/index.php').then(function (res){ that.user=res.data.name; //name为PHP端的JSON键 console.log(that.user) //打印输出到控制台把数值 }) }, } }) </script> ~~~ # 方法2利用箭头函数: ~~~ getDate:function (){ var that=this; //window转换为vue axios.get('http://127.0.0.1/index.php').then(res=>{ //箭头函数方法 that.user=res.data.name; //name为PHP端的JSON键 user为要输出的VUE里自定义的变量 console.log(that.user) //打印输出到控制台把数值 }) ~~~ ## 代码案例: ~~~ <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <title>Document</title> </head> <body> <div id="app"> <button type="button" @click="getDate">获取数据</button> <p>{{user}}</p> </div> </body> </html> <script type="text/javascript"> var vm= new Vue({ el:'#app', data:{ user:'1223' }, methods:{ getDate:function (){ var that=this; //window转换为vue axios.get('http://127.0.0.1/index.php').then(res=>{ //箭头函数方法 that.user=res.data.name; //name为PHP端的JSON键 console.log(that.user) //打印输出到控制台把数值 }) }, } }) </script> ~~~