“Vue的前后端通信(axios)”的版本间的差异
来自CloudWiki
(→前后端通信) |
|||
第21行: | 第21行: | ||
==前后端通信== | ==前后端通信== | ||
− | === | + | ===基本GET方法=== |
使用 response.data 读取 JSON 数据: | 使用 response.data 读取 JSON 数据: | ||
第108行: | 第108行: | ||
[[文件:vue2022041605.png|500px]] | [[文件:vue2022041605.png|500px]] | ||
+ | ===GET方法传参数=== | ||
+ | GET方法传参数有两种写法: | ||
+ | |||
+ | <nowiki> | ||
+ | // 直接在 URL 上添加参数 ID=12345 | ||
+ | axios.get('/user?ID=12345') | ||
+ | .then(function (response) { | ||
+ | console.log(response); | ||
+ | }) | ||
+ | .catch(function (error) { | ||
+ | console.log(error); | ||
+ | }); | ||
+ | |||
+ | // 也可以通过 params 设置参数: | ||
+ | axios.get('/user', { | ||
+ | params: { | ||
+ | ID: 12345 | ||
+ | } | ||
+ | }) | ||
+ | .then(function (response) { | ||
+ | console.log(response); | ||
+ | }) | ||
+ | .catch(function (error) { | ||
+ | console.log(error); | ||
+ | });</nowiki> | ||
+ | |||
+ | 具体代码: | ||
+ | |||
+ | <nowiki> | ||
+ | <!DOCTYPE html> | ||
+ | <html> | ||
+ | <head> | ||
+ | <meta charset="utf-8"> | ||
+ | <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title> | ||
+ | <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> | ||
+ | <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script> | ||
+ | </head> | ||
+ | <body> | ||
+ | <div id="app"> | ||
+ | {{ info }} | ||
+ | </div> | ||
+ | <script type = "text/javascript"> | ||
+ | new Vue({ | ||
+ | el: '#app', | ||
+ | data () { | ||
+ | return { | ||
+ | info: null | ||
+ | } | ||
+ | }, | ||
+ | mounted () { | ||
+ | axios | ||
+ | .get('http://127.0.0.1:5000/hello',{ | ||
+ | params: { | ||
+ | ID: 12345 | ||
+ | } | ||
+ | }) | ||
+ | .then(response => ( | ||
+ | this.info = response.data)) | ||
+ | .catch(function (error) { // 请求失败处理 | ||
+ | console.log(error); | ||
+ | }); | ||
+ | |||
+ | } | ||
+ | |||
+ | }) | ||
+ | </script> | ||
+ | </body> | ||
+ | </html> | ||
+ | |||
+ | |||
+ | </nowiki> | ||
+ | |||
+ | ====对应Flask代码==== | ||
+ | <nowiki> | ||
+ | import json | ||
+ | from flask import Flask,request | ||
+ | app = Flask(__name__)#创建一个该类的实例,第一个参数是应用模块或者包的名称 | ||
+ | from flask_cors import * | ||
+ | |||
+ | @app.route('/hello')#告诉Flask 什么样的URL能触发函数 | ||
+ | @cross_origin() | ||
+ | def hello_world(): | ||
+ | #return 'Hello World!' | ||
+ | s=['张三','年龄','姓名'] | ||
+ | t={} | ||
+ | t['ID'] = request.args.get('ID') | ||
+ | t['姓名']='张三' | ||
+ | t['年龄']='23' | ||
+ | t['职业']='码农' | ||
+ | data={} | ||
+ | data['SUCCESS']='SUCCESS' | ||
+ | data['data']=t | ||
+ | return json.dumps(data,ensure_ascii=False) | ||
+ | |||
+ | |||
+ | if __name__ == '__main__':#程序入口 | ||
+ | app.run()#让应用运行在本地服务器上。 | ||
+ | #app.run( host='0.0.0.0') #允许任意网址访问本站</nowiki> | ||
==参考文档== | ==参考文档== | ||
菜鸟教程:https://www.runoob.com/vue2/vuejs-ajax-axios.html | 菜鸟教程:https://www.runoob.com/vue2/vuejs-ajax-axios.html |
2022年4月16日 (六) 04:32的版本
axios简介
Vue.js 2.0 版本推荐使用 axios 来完成 ajax 请求。
Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中。
Github开源地址: https://github.com/axios/axios
环境准备
安装axios
使用 cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
或
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
后端准备
可参考Vue+Flask前后端传参,搭建一个Flask框架。
前后端通信
基本GET方法
使用 response.data 读取 JSON 数据:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script> </head> <body> <div id="app"> {{ info }} </div> <script type = "text/javascript"> new Vue({ el: '#app', data () { return { info: null } }, mounted () { axios .get('http://127.0.0.1:5000/hello') .then(response => (this.info = response.data)) .catch(function (error) { // 请求失败处理 console.log(error); }); } }) </script> </body> </html>
读取具体单个数据:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script> </head> <body> <div id="app"> {{ info }} </div> <script type = "text/javascript"> new Vue({ el: '#app', data () { return { info: null } }, mounted () { axios .get('http://127.0.0.1:5000/hello') .then(response => ( this.info = response.data.data['姓名'])) .catch(function (error) { // 请求失败处理 console.log(error); }); } }) </script> </body> </html>
GET方法传参数
GET方法传参数有两种写法:
// 直接在 URL 上添加参数 ID=12345 axios.get('/user?ID=12345') .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); // 也可以通过 params 设置参数: axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
具体代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title> <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script> </head> <body> <div id="app"> {{ info }} </div> <script type = "text/javascript"> new Vue({ el: '#app', data () { return { info: null } }, mounted () { axios .get('http://127.0.0.1:5000/hello',{ params: { ID: 12345 } }) .then(response => ( this.info = response.data)) .catch(function (error) { // 请求失败处理 console.log(error); }); } }) </script> </body> </html>
对应Flask代码
import json from flask import Flask,request app = Flask(__name__)#创建一个该类的实例,第一个参数是应用模块或者包的名称 from flask_cors import * @app.route('/hello')#告诉Flask 什么样的URL能触发函数 @cross_origin() def hello_world(): #return 'Hello World!' s=['张三','年龄','姓名'] t={} t['ID'] = request.args.get('ID') t['姓名']='张三' t['年龄']='23' t['职业']='码农' data={} data['SUCCESS']='SUCCESS' data['data']=t return json.dumps(data,ensure_ascii=False) if __name__ == '__main__':#程序入口 app.run()#让应用运行在本地服务器上。 #app.run( host='0.0.0.0') #允许任意网址访问本站