我需要从http响应中访问"status“、"message”等。在chrome浏览器的“网络”标签中,我确实看到了我所需要的一切。但是在"subscribe“方法中,返回的错误并没有定义这些属性。
deleteBranchByID(branchID: string) {
return this.http.delete(this.baseUrl + 'api/Branch/DeleteBranchByID/' + branchID);
}"Error“不包含我需要的属性...
onDelete(branchID) {
this.branchesService.deleteBranchByID(branchID)
.subscribe(
(res) => {
console.log(res);
},
(error) => {
console.log(error);
}
);
}...instead,console.log(error)返回如下内容:
TypeError: Cannot read property 'message' of undefined
at CatchSubscriber.selector (common-auth.js:945)
at CatchSubscriber.error (catchError.js:29)
at CatchSubscriber.notifyError (OuterSubscriber.js:7)
at InnerSubscriber._error (InnerSubscriber.js:14)
at InnerSubscriber.error (Subscriber.js:55)
at Observable._Observable__WEBPACK_IMPORTED_MODULE_0__.Observable.scheduler.schedule.error.error [as _subscribe] (throwError.js:4)
at Observable._trySubscribe (Observable.js:42)
at Observable.subscribe (Observable.js:28)
at subscribeTo.js:20
at subscribeToResult (subscribeToResult.js:7)这些相同的方法在另一个项目中可以完美地工作,但在这里不是。
发布于 2021-04-26 23:30:32
添加observe: 'response'对象以从响应获取对status的访问。
deleteBranchByID(branchID: string) {
return this.http.delete(this.baseUrl + 'api/Branch/DeleteBranchByID/' + branchID, { observe: 'response' });
}我认为你可能会得到一个200状态码,但是响应是空的,因此http调用失败了,但这只是一个猜测。
查看this,看看如何从响应中获取头部、状态代码等,而不仅仅是正文。
https://stackoverflow.com/questions/67268977
复制相似问题