问题:点击按钮后要获取到后台python脚本返回的值,还是太小白啊~
解决:用ajax
temp.html:
<html>
<head>
<meta charset="utf-8">
<title>TEMP</title>
</head>
<body>
<input type="button" value="微笑" onclick="hello()"/>
<p id="display">{{ temp }}</p>
</body>
<script type="text/javascript">
function hello()
{
$.ajax(
{
type: "POST",
url: "/hello",
dataType: "html",
success: function(msg)
{
console.log(msg);
$("#display").html(msg);//注意显示的id
},
error: function (xhr, status, error) {
console.log(error);
}
});
}
</script>
</html>
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/hello", methods=['GET', 'POST'])
def Hello():
message = "hello"
return render_template("temp.html", temp=message)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8987, debug=True)