python使用flask與js進行前后臺交互的例子
更新時間:2019年07月19日 11:34:23 作者:小明37
今天小編就為大家分享一篇python使用flask與js進行前后臺交互的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
flask與js進行前后臺交互代碼如下,后臺給前端發(fā)數(shù)據(jù):
python部分:
# -*- coding: utf-8 -*- from flask import Flask,jsonify,render_template import json app = Flask(__name__)#實例化app對象 testInfo = {} @app.route('/test_post/nn',methods=['GET','POST'])#路由 def test_post(): testInfo['name'] = 'xiaoming' testInfo['age'] = '28' return json.dumps(testInfo) @app.route('/') def hello_world(): return 'Hello World!' @app.route('/index') def index(): return render_template('index.html') if __name__ == '__main__': app.run(host='0.0.0.0',#任何ip都可以訪問 port=7777,#端口 debug=True )
js部分:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>echarts</title> <style type="text/css"> html, body { width: 100%; height: 100%; } body { margin: 0px; padding: 0px } div { float: left; } #container { width: 50%; height: 100%; } #info { padding: 10px 20px; } </style> </head> <body> <div id="container"></div> <div id="info">數(shù)據(jù)展示:</div> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script> <script> $.ajax({ url: "test_post/nn", type: "POST", dataType: "json", success: function (data) { console.log(data) } }) </script> </body> </html>
以上這篇python使用flask與js進行前后臺交互的例子就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python中實現(xiàn)迭代器(iterator)的方法示例
我們經(jīng)常需要遍歷一個對象中的元素,在Python中這種功能是通過迭代器來實現(xiàn)的。下面這篇文章主要給大家介紹了python中實現(xiàn)迭代器(iterator)的方法示例,需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01Python入門之三角函數(shù)tan()函數(shù)實例詳解
這篇文章主要介紹了Python入門之三角函數(shù)tan()的相關內容,介紹了tan()函數(shù)的描述,語法以及簡單實例,具有一定參考價值,需要的朋友可以了解下。2017-11-11Python issubclass和isinstance函數(shù)的具體使用
本文主要介紹了Python issubclass和isinstance函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02pandas factorize實現(xiàn)將字符串特征轉化為數(shù)字特征
今天小編就為大家分享一篇pandas factorize實現(xiàn)將字符串特征轉化為數(shù)字特征,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12