所以我正在向 nodeJS 應用程式發送一個 POST 請求,我在 Angular 中的請求如下所示:
newWord = '';
keyword = '';
onClick() {
const headers = new HttpHeaders()
.set('Authorization', 'my-auth-token')
.set('Content-Type', 'application/json');
this.http
.post('http://localhost:3000/search',{
keyword: this.keyword,
responseType: 'text',
headers: headers,
})
.subscribe((data) => {
console.log(data);
this.newWord = data as string;
})
}
我的 Node 應用程式發布回應設定如下:
app.post("/search", (req, res) => {
search(req.body.keyword).then((ids) => {
console.log(ids[0].text);
res.send(ids[0].text);
});
});
我的問題是在本地主機中打開控制臺時出現此錯誤

uj5u.com熱心網友回復:
您的 HTTP POST 請求需要更改(如評論中的@Drenai 和 @MikeOne 所述):
this.http.post('http://localhost:3000/search',
{keyword: this.keyword},
{responseType: 'text',
headers: headers,
})
.subscribe((data) => {
console.log(data);
this.newWord = data as string;
})
uj5u.com熱心網友回復:
我可以看到您剛剛以文本形式發送了回應,因此在客戶端嘗試決議回應正文給出問題。
所以在節點本身發送 JSON 格式的回應我希望它有所幫助。
app.post("/search", (req, res) => {
search(req.body.keyword).then((ids) => {
console.log(ids[0].text);
**res.send({response:ids[0].text});**
});
this.http
.post('http://localhost:3000/search',{
keyword: this.keyword,
headers: headers,
responseType: 'text',
})
.subscribe((data) => {
console.log(data['reponse']);
this.newWord = data['response'];
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/498358.html
