使用ajax在发布json后更改HTML内容失败
我正在用flask写一个小网站,用邮递员发送发帖请求 app.py使用ajax在发布json后更改HTML内容失败,ajax,flask,Ajax,Flask,我正在用flask写一个小网站,用邮递员发送发帖请求 app.py @app.route('/index', methods=['GET', 'POST']) def index(): if session["logged_in"]: return render_template('index.html') else: return redirect(url_for('login')) @app.route('/get_msg', methods
@app.route('/index', methods=['GET', 'POST'])
def index():
if session["logged_in"]:
return render_template('index.html')
else:
return redirect(url_for('login'))
@app.route('/get_msg', methods = ['POST'])
def get_msg():
time_toshow = request.get_json(force = True)
return time_toshow
html(主要部分)
控制台也不记录任何内容请尝试以下操作:
$.ajax({
url: "{{url_for('get_msg')}}",
type: 'post',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(time_toshow),
success: function (data) {
$('#shown').html(JSON.stringify(data));
console.log(data);
},
});
请注意JSON.stringify(),在success函数中以及在传递数据时都是如此
如果不使用JSON.stringify(),则JSON将转换为normaltime=5000而不是{“normaltime”:“5000”},因此服务器返回400错误(错误请求)。看
{"AbnormalTime": "5000"}
$.ajax({
url: "{{url_for('get_msg')}}",
type: 'post',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(time_toshow),
success: function (data) {
$('#shown').html(JSON.stringify(data));
console.log(data);
},
});