首页 > 解决方案 > Angular5 + HttpClient + Observable + HTTP PUT + 204 无内容

问题描述

我有一个 webapi ,它具有 HTTP PUT 方法,返回结果为“204 No content”(如果成功)。我尝试使用 Angular5 + HttpClient + Observable 来更新数据。我的代码是:

updateUser(user): Observable<object> {
var putUrl = this.userURL + "/" + user.id;
var result = this.http.put(putUrl, user).pipe(
  catchError(this.handleError)
);
return result;}


private handleError(error: HttpErrorResponse) {
    if (error.error instanceof ErrorEvent) {
      // A client-side or network error occurred. Handle it accordingly.
      console.error('An error occurred:', error.error.message);
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong,
      console.error(
        `Backend returned code ${error.status}, ` +
        `body was: ${error.error}`);
    }
    // return an observable with a user-facing error message
    return _throw(
      'Something bad happened; please try again later.');
  };

我点击了更新按钮,在网络选项卡中没有对服务器的请求,也没有控制台错误。我哪里错了?

更新我太愚蠢了,对不起。我应该订阅()结果

标签: angular5

解决方案


愚蠢的问题,我很着急。应该:this.userService.updateUser(user).subscribe(); 一切都会正常工作。


推荐阅读