学习总结
@app.route(’/user’) @app.route(’/user/’),通过“/”两种写法,解决访问链接的相同返回页面;
@app.route(’/userinfo’, methods=[‘get’, ‘post’]),get和post参数获取与传递;
{{ url_for(‘static’,filename=‘js/jquery-3.3.1.min.js’) }},静态页面加载文件的写法;
路由装饰器
# ajax互动-1 @app.route('/user') @app.route('/user/') def get_user(): return render_template('user.html') @app.route('/userinfo', methods=['get', 'post']) def get_userinfo(): name = request.values.get('name') ages = request.values.get('ages') str_list = "姓名:{},年龄:{}".format(name, ages) return str_list
html文档
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>ajax参数传递互动</title> <!--静态文件引入--> <link rel="stylesheet" href="{{ url_for('static',filename='css/common.css') }}"> <script src="{{ url_for('static',filename='js/jquery-3.3.1.min.js') }}"></script> <script src="{{ url_for('static',filename='js/echarts.min.js') }}"></script> </head> <body> <div id="container"></div> <script> $(function(){ $.ajax({ url: "/userinfo", type: "get", data: {"name": "lockdatav", "ages": 36}, success: function (data) { $("#container").html(data); }, error: function (err) { console.log(JSON.stringify(err)); } }) }) </script> </body> </html>
lockdatav Done!