python使用flask与js进行前后台交互的例子
flask与js进行前后台交互代码如下,后台给前端发数据:
python部分:
#-*-coding:utf-8-*-
fromflaskimportFlask,jsonify,render_template
importjson
app=Flask(__name__)#实例化app对象
testInfo={}
@app.route('/test_post/nn',methods=['GET','POST'])#路由
deftest_post():
testInfo['name']='xiaoming'
testInfo['age']='28'
returnjson.dumps(testInfo)
@app.route('/')
defhello_world():
return'HelloWorld!'
@app.route('/index')
defindex():
returnrender_template('index.html')
if__name__=='__main__':
app.run(host='0.0.0.0',#任何ip都可以访问
port=7777,#端口
debug=True
)
js部分:
echarts html, body{ width:100%; height:100%; } body{ margin:0px; padding:0px } div{ float:left; } #container{ width:50%; height:100%; } #info{ padding:10px20px; }