首页 > 解决方案 > 烧瓶请求返回无;但是 Ajax Post 是成功的

问题描述

在烧瓶中,我使用 GET 请求首先呈现模板并获取用户选择的数据。

@app.route('/surveys', methods=['POST', 'GET'])
def register_form():
    if request.method == 'GET':
        return render_template('survey_table.html')
    else:
        example = request.json
        return json.dumps(example)

然后使用 ajax Post 请求将此数据(即表中选中的复选框的 #ids)发回 FLASK。由于数据最初是一个数组,因此我使用了 JSON.stringify

    $(document).ready(function() {
    $('#submit').click(function() {
        var list = [];
        var checkBoxes = $('#surveyDetails').find("input[type='checkbox']:checked");
        checkBoxes.each(function() {
        var currentRow = this.parentNode.parentNode;
        list.push(currentRow.getElementsByTagName("td")[0].innerText);
        });
        // now names contains all of the names of checked checkboxes
        $.ajax({
            contentType: "application/json",
            url: '/surveys',
            type: 'POST',
            data: JSON.stringify({'Hello': list}),
            success: function (result) {
                alert(result);
            },
            error: function (result) {
                alert("error!");
            }
        });   //end ajax
    });
});

问题:

我可以看到 ajax 帖子已成功执行,因为一旦我单击提交按钮,我就可以看到选定的 id 作为警报。但是,当我返回这个 JSON 对象时,在烧瓶中,我总是得到一个空响应。

我已经尝试过 get_json(force=True)。结果还是一样。

标签: pythonjqueryajaxflask

解决方案


推荐阅读