瓶子 post 未返回响应
Bottle post not returning response
我的后端使用 Bottle,前端使用 Angular2。当使用 POST 请求时,我想 return 响应,但由于某种原因,它没有 return 响应
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
Angular代码:
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: ""
我推荐你使用HttpClient
。为此,导入 HttpClientModule
,您的代码可能是:
constructor(private httpClient: HttpClient) {}
load() {
this.http.post(this.baseurl, {
"h": h,
"d": des,
"t": Date.now()
}, {
observe: 'response',
headers: new HttpHeaders({
'Content-Type': 'application/json'
}
}).subscribe(response => {
// do stuff with body
// response.body
// this will output headers received
const keys = response.headers.keys();
const headers = keys.map(key =>
`${key}: ${response.headers.get(key)}`);
console.table(headers);
})
}
如果您不需要 response
详细信息,但只有 body
收到:
this.http.post<MyData>(this.baseurl, {
"h": h,
"d": des,
"t": Date.now()
}).subscribe(data => {
// do stuff with data of type MyData
data.name...
});
有关 Angular official docs 的更多详细信息。
我的后端使用 Bottle,前端使用 Angular2。当使用 POST 请求时,我想 return 响应,但由于某种原因,它没有 return 响应
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
Angular代码:
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: ""
我推荐你使用HttpClient
。为此,导入 HttpClientModule
,您的代码可能是:
constructor(private httpClient: HttpClient) {}
load() {
this.http.post(this.baseurl, {
"h": h,
"d": des,
"t": Date.now()
}, {
observe: 'response',
headers: new HttpHeaders({
'Content-Type': 'application/json'
}
}).subscribe(response => {
// do stuff with body
// response.body
// this will output headers received
const keys = response.headers.keys();
const headers = keys.map(key =>
`${key}: ${response.headers.get(key)}`);
console.table(headers);
})
}
如果您不需要 response
详细信息,但只有 body
收到:
this.http.post<MyData>(this.baseurl, {
"h": h,
"d": des,
"t": Date.now()
}).subscribe(data => {
// do stuff with data of type MyData
data.name...
});
有关 Angular official docs 的更多详细信息。