我正在使用Angular的新HttpClient来处理请求。当我使用旧的http时,我的组件返回正文文本,但现在正文文本为空。我无法弄明白,服务器正在发送它,但我总是收到null作为正文。
响应的每个其他部分都是正常的,状态为200等。放置和删除都需要明文成功消息。我也尝试使用.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' });