python - 烧瓶请求返回无;但是 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)。结果还是一样。
解决方案
推荐阅读
- swift - 如何将图像方向应用于 CIImage?
- javascript - 在 html 中设置一个变量以用作图像 src
- c - 关于 char *s 和 char s[] 如何在低级别工作的困惑
- javascript - 是否有任何内置/简单的方法可以让字符串不被传输?
- python - Python:读写文件时出现问题
- javascript - 从 typescript 中作为 prop 提供的数组分配样式
- mongodb - 有没有办法计算 mongodb 中每个集合的读/写?
- kotlin - 如何从不同的功能发出流量值?Kotlin 协程
- android-studio-3.6 - 戴尔电脑上的安卓工作室看不到我的安卓设备
- php - 具有来自功能属性的静态值的 jQuery 发布表单