我在前端使用JS从python flask后端获取数据。
fetch("/get_masks", {
method : "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({"image_index" : 0})
}).then(response => response.json()).then( data => {
console.log(data["hello"]);
});
瓶:
@app.route("/get_masks", methods=["POST"])
def get_masks():
return jsonify(hello="test")
这工作得很好,但如果我在响应中添加括号和分号,它就不再工作了
.then(response => {response.json();}).then( data => {
console.log(data["hello"]);
那么"response"返回一个未定义的对象。
有人知道为什么会这样吗?
提前感谢!
为简单起见,response => response.json()
即response => { return response.json(); }
。
所以,如果你想用大括号,用return
。
.then(response => {
return response.json();
})
.then( data => {
console.log(data["hello"]);
})