【发布时间】:2020-03-25 02:14:11
【问题描述】:
我使用 Bottle 作为后端,使用 Angular2 作为前端。使用 POST 请求时,我想返回响应,但由于某种原因,它没有返回响应
Python 代码:
@app.post("/api/post/new_post")
def hp_new_post():
pd = json.loads(request.body.read().decode("utf-8"))
try:
cl.eduo.posts.insert_one(
{
"h": pd['h'],
"d": pd['d'],
"t": pd['t']
}
)
except Exception:
response.status = 500
response.content_type = 'application/json'
response.status = 200
return response
角度代码:
var result = from(
fetch(
this.baseurl,
{
body: JSON.stringify(
{
"h": h,
"d": des,
"t": Date.now()
}),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
mode: 'no-cors'
}
)
).subscribe(
{
next(data) {
console.log(data);
},
error(msg) {
console.log(msg);
}
}
)
当我从一个请求中获得 console.log 数据时:
body: null
bodyUsed: false
headers: Headers { }
ok: false
redirected: false
status: 0
statusText: ""
type: "opaque"
url: ""
【问题讨论】:
-
看起来这更像是在服务器(python)端而不是角度。您是否尝试过在您的 python 应用程序中配置 cors?阅读这些答案可能会有所帮助:stackoverflow.com/questions/36292537/…
标签: python-3.x angular rxjs bottle