我正在使用 Angular 的新 HttpClient 来处理请求。当我使用旧的 http 时,我的组件可以很好地返回正文文本,但现在正文文本为空。我无法弄清楚,服务器正在发送它,但我总是收到 null 作为正文。
响应的其他部分都是正常的,状态 200 等。put 和 delete 都需要纯文本成功消息。我也尝试过使用 .body 方法来获取正文文本,它也返回 null。
服务:
constructor(private http: HttpClient) {}
sendAllow(country, allow) {
if (allow === 1) {
return this.http.put(`http://${this.address}/rest/geodrop/config/` +
`${country}`, { observe: 'response' });
} else {
return this.http.delete(`http://${this.address}/rest/geodrop/config/` +
`${country}`, { observe: 'response' });
}
}
组件:
this.whiteListService.sendAllow(a, b)
.subscribe(
(response) => console.log(response),
(error) => {
console.log(error);
}
);
最佳答案
问题是 Angular 需要一个 json 响应。更改响应以键入文本,如下所示:
return this.http.put(`http://${this.address}/rest/geodrop/config/` +
`${country}`, { responseType: 'text', observe: 'response' });
关于angular - 响应主体为空,状态为 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46064914/