响应正文为空,状态为200

问题描述:

我正在使用Angular的新HttpClient进行请求。当我使用旧的http时,我的组件返回正文文本很好,但现在正文文本为空。我无法弄清楚,服务器正在发送它,但我总是收到null作为正文。响应正文为空,状态为200

响应的每个其他部分是正常的,状态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); 
       } 
     ); 
+0

有2个HTTP请求,一个'PUT'和一个'DELETE',哪一个返回了意外的'null'文本?什么是预期的正文文本? – shaochuancs

+0

两者都应该返回一个纯文本成功消息用于放入或删除 – FussinHussin

+0

URL是否公开?我们可以尝试使用“PUT”还是“DELETE”请求? – shaochuancs

问题是角期待的JSON响应。更改类型的文字的响应如下:

return this.http.put(`http://${this.address}/rest/geodrop/config/` + 
        `${country}`, { responseType: 'text', observe: 'response' }); 
+0

太棒了!这工作完美 –

+1

很高兴它帮助哈哈。我想,当我发布它时,这是一个愚蠢的问题,因为它没有牵引力。我自己想出来了,几个月后它帮助了人们。很高兴知道这不是无用的 – FussinHussin

+0

谢谢你在将接受/内容标题更改为'文本'后抓住我的头,但在订阅中获得空值! –