python使用flask與js進(jìn)行前后臺(tái)交互的例子
更新時(shí)間:2019年07月19日 11:34:23 作者:小明37
今天小編就為大家分享一篇python使用flask與js進(jìn)行前后臺(tái)交互的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
flask與js進(jìn)行前后臺(tái)交互代碼如下,后臺(tái)給前端發(fā)數(shù)據(jù):
python部分:
# -*- coding: utf-8 -*-
from flask import Flask,jsonify,render_template
import json
app = Flask(__name__)#實(shí)例化app對(duì)象
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進(jìn)行前后臺(tái)交互的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 用所有標(biāo)點(diǎn)符號(hào)分隔句子的示例
今天小編就為大家分享一篇python 用所有標(biāo)點(diǎn)符號(hào)分隔句子的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07
pytorch中使用cuda擴(kuò)展的實(shí)現(xiàn)示例
這篇文章主要介紹了pytorch中使用cuda擴(kuò)展的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
解決Python正則表達(dá)式匹配反斜杠''''\''''問題
這篇文章主要介紹了Python正則表達(dá)式匹配反斜杠'\'問題 ,很多朋友在使用python 正則式的過程中,經(jīng)常被這個(gè)問題困擾,今天小編通過代碼給大家詳細(xì)介紹,需要的朋友可以參考下2019-07-07
Python中處理unchecked未捕獲異常實(shí)例
這篇文章主要介紹了Python中處理unchecked未捕獲異常實(shí)例,本文講解使用回調(diào)或者是鉤子來處理unchecked異常,需要的朋友可以參考下2015-01-01

