Javascript 角度HttpClient.put可观测返回null
我订阅了由我的Javascript 角度HttpClient.put可观测返回null,javascript,angular,typescript,observable,Javascript,Angular,Typescript,Observable,我订阅了由我的HttpClient.put(…)返回的Observable,响应返回null。这是我的密码: constructor(private http: HttpClient) { } httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; updateIsEnabled(type: string, isChecked: boolean) { const u
HttpClient.put(…)
返回的Observable
,响应返回null
。这是我的密码:
constructor(private http: HttpClient) { }
httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
updateIsEnabled(type: string, isChecked: boolean) {
const url = `${this.isEnabledUrl}?type=${type}&isEnabled=${isChecked}`;
this.http.put(url, this.httpOptions)
.pipe(
catchError(this.handleError('updateIsEnabled'))
).subscribe(response => {
if (response.getStatusCode() == 200) {
//do something
}
});
}
但我得到:
错误类型错误:无法读取null的属性“getStatusCode”
我确信我的响应是200ok
(Spring
后端),但主体是空的。我想它可能期待一个JSON
。因此,我将选项更改为:
httpTextOptions = {
headers: new HttpHeaders({ 'Content-Type': 'text/plain',
'Content-Length': '0'})
}
但仍然有相同的结果
有什么想法吗
谢谢我不明白catchError的意义,因为它破坏了可观察的对象,不确定它是一个bug还是我没有正确理解它,但我已经学会了不要使用它,因为它对捕捉错误没有用处
const{throwError}=rxjs;
const{catchError}=rxjs.operators;
抛掷器(“错误”)。管道(
catchError(e=>{console.log(e);})
).subscribe(val=>{console.log(val);},e=>{console.log(e);})代码>
观察响应即使您确实遵循了获取响应的可观察性而不是解析的主体,也没有这样的方法。你为什么认为这会奏效?@jonrsharpe这不是重点。如果我写response.abcdefg()
,你会提出来吗?当然,我也会有同样的问题。除了jonrsharpe的问题,我同意这个问题。。。您是否已检查“网络”选项卡以查看正在接收的内容?似乎响应中没有任何值,如错误所示。