angular5 - 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.');
};
我点击了更新按钮,在网络选项卡中没有对服务器的请求,也没有控制台错误。我哪里错了?
更新我太愚蠢了,对不起。我应该订阅()结果
解决方案
愚蠢的问题,我很着急。应该:this.userService.updateUser(user).subscribe(); 一切都会正常工作。
推荐阅读
- python - 存储和引用类方法的 Pythonic 方式
- arrays - 使用 Nodejs 在 mongoDB 中的数组字段上搜索 $text $search
- angular - 检查变量中的值变化
- angular - 如何在 Angular 中向 Web 服务调用 URL 的路径变量注入值?
- php - 如何根据ID获取多维数组的值
- node.js - 如何在node js中配置redis客户端
- javascript - 昨天对象 HTMLSpanElement 我的代码运行良好,但现在我的银值显示了这一点
- php - 如何使用 preg_match 匹配脚本?
- php - Laravel 路由没有像他们应该的那样工作
- python - 引发异常 xml.sax._exceptions.SAXParseException:
:1:0: 格式不正确(无效令牌)