render_模板在接收ajax内容(flask、python、javascript)后不执行任何操作
当满足以下条件时,我将使用ajax将数据传递给python函数:render_模板在接收ajax内容(flask、python、javascript)后不执行任何操作,javascript,python,ajax,web,flask,Javascript,Python,Ajax,Web,Flask,当满足以下条件时,我将使用ajax将数据传递给python函数: if (lesson.length === 0) { $.ajax( { type:'POST', contentType:'application/json', dataType:'json', ur
if (lesson.length === 0) {
$.ajax(
{
type:'POST',
contentType:'application/json',
dataType:'json',
url:'http://127.0.0.1:5000/result?value=' + errors ,
success:function(response){ document.write(response); }
}
);
}
我知道信息接收正确,因为我可以通过打印在终端上看到:
127.0.0.1 - - [19/Aug/2020 11:59:46] "GET /static/flexjava.js HTTP/1.1" 200 -
0
127.0.0.1 - - [19/Aug/2020 11:59:48] "POST /result?value=0 HTTP/1.1" 200 -
但是python在print()函数之后什么也不做。渲染或重定向都不起作用,即使传递了信息,浏览器仍保持原样:
@app.route("/result", methods=["GET", "POST"])
def result():
content = request.args.get('value')
if "username" not in session or session["username"] == "guest":
return redirect("/login")
if request.method == "GET":
return redirect("/")
else:
print(content)
return render_template("finished.html")
您没有正确使用ajax。您希望返回一个
json
响应,而不是一个完整的网页
尝试:
然后:
此功能实际上什么也不做,因为调用模板中已经有错误。如果您试图转到finished
,您可以在ajaxsuccess
回调中执行此操作:
success:function(response){
console.log(response);
window.location.replace(window.location.href + "finished");
}
from flask import jsonify
@app.route("/result", methods=["GET", "POST"])
def result():
content = request.args.get('value')
...
else:
print(content)
return jsonify(
{
"content": content
}
)
success:function(response){
console.log(response);
window.location.replace(window.location.href + "finished");
}