Flask请求返回None;但是Ajax Post是成功的

时间:2018-05-07 06:25:25

标签: python jquery ajax flask

在烧瓶中,我正在使用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请求将此数据(在表中选择的复选框的#id)发布回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)。结果仍然相同。

0 个答案:

没有答案
相关问题