我正在使用Angular的新HttpClient进行请求。当我使用旧的http时,我的组件返回正文文本,但现在正文文本为空。我无法弄清楚,服务器正在发送它,但我总是收到null作为正文。
响应的每个其他部分都是正常的,状态为200等。放置和删除都需要明文成功消息。我也尝试使用.body方法获取正文文本,并返回null。
服务:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<AvailableBuilds>
<BuildGroup>
<Type>working</Type>
<Build>
</Build>
<Build>
<VersionTag>wc-853</VersionTag>
<DateBuilt>2017-08-24T20:50:55Z</DateBuilt>
<ExecutableURL>http://example.com/ServerApps/Java/Crossbow/trunk/wc-853/Crossbow_wc-853.zip</ExecutableURL>
<SupportURL>http://example.com/ServerApps/Java/Crossbow/trunk/wc-853/Crossbow_wc-853_etc.zip</SupportURL>
<NotesURL>http://example.com/ServerApps/Java/Crossbow/trunk/wc-853/Crossbow_wc-853_notes.txt</NotesURL>
</Build>
成分:
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' });
}
}
答案 0 :(得分:12)
问题是Angular期待json响应。将响应更改为类型文本,如下所示:
return this.http.put(`http://${this.address}/rest/geodrop/config/` +
`${country}`, { responseType: 'text', observe: 'response' });