Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度HttpClient.put可观测返回null_Javascript_Angular_Typescript_Observable - Fatal编程技术网

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的问题,我同意这个问题。。。您是否已检查“网络”选项卡以查看正在接收的内容?似乎响应中没有任何值,如错误所示。