Vue和Flask通信的實現(xiàn)
安裝axios和實現(xiàn)通信
這里我們通過axios來連接Vue前端和Flask后端,使用AJAX請求進(jìn)行通信。使用如下命令安裝
npm install axios
axios的使用格式:
import axios from 'axios'; export default { data: function () { return { serverResponse: 'res_test' }; }, methods: { getData() { // 設(shè)置對應(yīng)python的接口,這里使用的是localhost:5000 const path = 'http://127.0.0.1:5000/getMsg'; // 這里要使用 res =>表示返回的數(shù)據(jù) axios.get(path).then(res => { // 這里服務(wù)器返回response為一個json對象 // 通過.data來訪返回的數(shù)據(jù),然后在通過.變量名進(jìn)行訪問 // 可以直接通過response.data取得key-value var msg = res.data.msg; this.serverResponse = msg; // 因為不能直接使用this作為指針,因此在這之前將this賦給了then指針 alter('Success' + response.status + ',' + response.data + ',' + msg); // 成功后顯示提示 }).catch(error => { console.error(error); }); } }, }
代碼及演示
前端代碼
對./components/HelloWorld.vue文件進(jìn)行改寫。代碼如下:
<!-- html部分 --> <template> <div> <span>{{ serverResponse }}</span> <!--這里使用{{}}來引用JavaScript中賦給this的值--> <button @click="getData">get data</button> </div> </template> <!-- js部分 --> <script> import axios from 'axios'; export default { data: function () { return { serverResponse: 'res_test' }; }, methods: { getData() { // 設(shè)置對應(yīng)python的接口,這里使用的是localhost:5000 const path = 'http://127.0.0.1:5000/getMsg'; axios.get(path).then(res => { // 這里服務(wù)器返回response為一個json對象 // 通過.data來訪返回的數(shù)據(jù),然后在通過.變量名進(jìn)行訪問 // 可以直接通過response.data取得key-value var msg = res.data.msg; this.serverResponse = msg; // 因為不能直接使用this作為指針,因此在這之前將this賦給了then指針 alter('Success' + response.status + ',' + response.data + ',' + msg); // 成功后顯示提示 }).catch(error => { console.error(error); }); } }, } </script> <!-- css部分 --> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> h1, h2 { font-weight: normal; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; } </style>
這里主要實現(xiàn)了通過單擊按鈕來和服務(wù)器端進(jìn)行交互獲得數(shù)據(jù)并傳回前端,將得到的數(shù)據(jù)重新來對前端進(jìn)行渲染。
得到如上頁面之后,我們單擊get date按鈕,就會像后端發(fā)送GET請求,后端服務(wù)器監(jiān)聽到請求之后就會返回對應(yīng)的數(shù)據(jù)。
客戶端代碼
from flask import Flask from flask import jsonify from flask_cors import CORS app = Flask(__name__) cors = CORS(app, resources={r"/getMsg": {"origins": "*"}}) @app.route('/') def hello_world(): return 'test!' # 監(jiān)聽127.0.0.1:5000/getMsg請求 @app.route('/getMsg', methods=['GET', 'POST']) def home(): response = { 'msg': 'Hello, Python !' } return response if __name__ == '__main__': app.run()
到此這篇關(guān)于Vue和Flask通信的實現(xiàn)的文章就介紹到這了,更多相關(guān)Vue和Flask通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項目input標(biāo)簽checkbox,change和click綁定事件的區(qū)別說明
這篇文章主要介紹了vue項目input標(biāo)簽checkbox,change和click綁定事件的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08淺談Vue中的this.$store.state.xx.xx
這篇文章主要介紹了Vue中的this.$store.state.xx.xx用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09vue watch自動檢測數(shù)據(jù)變化實時渲染的方法
本篇文章主要介紹了vue watch自動檢測數(shù)據(jù)變化實時渲染的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01通過vue提供的keep-alive減少對服務(wù)器的請求次數(shù)
這篇文章主要介紹了通過vue提供的keep-alive減少對服務(wù)器的請求次數(shù),文中給大家補充介紹了vue路由開啟keep-alive時的注意點,需要的朋友可以參考下2018-04-04