响应正文为空,状态为200 [英] Response body is null, status is 200
问题描述
我正在使用Angular的新HttpClient进行请求.当我使用旧的http时,我的组件返回的主体文本很好,但是现在主体文本为null.我无法弄清楚,服务器正在发送它,但是我总是收到null作为正文.
I'm using Angular's new HttpClient for requests. When I was using the old http, my component returned the body text just fine, but now the body text is null. I can't figure it out, the server is sending it, but i'm always receiving null as the body.
响应的其他所有部分都正常,状态为200,等等.put和delete都希望收到一条纯文本成功消息.我也尝试过使用.body方法获取正文,该正文也返回null.
every other part of the response is normal, status 200 etc. Both the put and the delete expect a plain text success message. I have also tried using the .body method to get the body text, and that returns null as well.
服务:
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响应.将响应更改为键入文本,如下所示:
The problem is Angular is expecting a json response. change the response to type text like so:
return this.http.put(`http://${this.address}/rest/geodrop/config/` +
`${country}`, { responseType: 'text', observe: 'response' });
这篇关于响应正文为空,状态为200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!