angular - 响应主体为空,状态为 200

标签 angular http httpclient

我正在使用 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/

相关文章:

javascript - 将 http get 调用从 Angular 2 转换为最新版本

javascript - Angular 2 : A property set during OnInit is undefined on the template

java 11 HttpClient 导致无休止的 SSL 循环

http - 当浏览器在请求之间关闭或刷新时,网络服务器会发生什么?

.net - HTTP/.NET : How do I post some data and then work within that session?

php - 如何将 "body"参数发送到HttpClient.get()?

angular - 如何使用与angular2 http服务的保持连接

html - Angular 2 : Expression containing markup

javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated when trying to session. getPeerCertificateChain()

java - 使用 Java 登录 SiteMinder