Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ajax在发布json后更改HTML内容失败_Ajax_Flask - Fatal编程技术网

使用ajax在发布json后更改HTML内容失败

使用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

我正在用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 = ['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);
    },
});