【发布时间】:2020-12-28 03:35:35
【问题描述】:
我已经被困了半天了,因为我不确定为什么在一个控制台上它是成功的,而在另一个控制台上却没有。
我正在尝试从 chrome 扩展/客户端向烧瓶服务器发起 POST 调用。
background.js
fetch('http://0.0.0.0:5000/test',
{
method:'POST',
body: JSON.stringify({"name":"daniel"}),
headers: {
"Content-Type": "application/json;charset=UTF-8",
'Accept': 'application/json'
}
})
.then(response => response.json())
.then(json => console.log(JSON.stringify(json)));
manifest.json
{
"manifest_version": 2,
"name": "To be named",
"description": "This extension helps...",
"version": "0.1.0",
"permissions": [
"identity",
"identity.email",
"http://0.0.0.0:5000/test"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [{
"matches": ["https://www.youtube.com/"],
"js": ["content.js"],
"css": ["styles.css"]
}]
}
PY(烧瓶)
@blueprint.route('/test', methods=['GET', 'POST'])
def test():
if request.method == 'POST':
return request.json#{"status": "OK", "status_code": 200, "data": data}
elif request.method == 'GET':
return request.json
else:
return 'nothing printed'
目前尝试的解决方案:
-
request.loads/dumpts(request.json)
在烧瓶上安装 CORS
将 Content-Type 更改为 Content-'t'type
^所有这些解决方案只是返回 null 作为响应而不是错误,并且没有得到后台控制台中显示的实际数据
后台js控制台: enter image description here
测试开发:
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
在控制台上:
Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)
【问题讨论】:
标签: javascript http flask google-chrome-extension fetch