我正在使用AJAX将JSON字符串发布到Python Flask,但是该字符串似乎没有传递到flask应用程序。
scoresaver.addEventListener('click', function(ev){
if (scorestate==1) {
var pdata = {'uname':uname, 'score':score.saved}
$.ajax({
type: 'POST',
url: '/',
data: JSON.stringify(pdata),
})
score.saved=null;
scorestate=0;
}
});
单击按钮时,以上代码应该将JavaScript obj作为字符串“发送”到Python Flask。我已经验证JSON.stringify(pdata)产生了我需要的字符串。
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
print(request.get_json())
return render_template("index.html")
else:
return render_template("index.html")
这是我在Flask应用程序打印中的代码(request.get_json()变为None。
答案 0 :(得分:0)
请将此密钥添加到您的ajax调用中,然后重试:
contentType: "application/json",
仅当内容类型设置为JSON时,才会填充请求的get_json()
。