我有一个POST调用来从我的角度服务中休息api,我想要返回错误状态或错误消息来对它做一些逻辑,但我总是将[Object]作为响应(或未定义,取决于我从错误处理方法返回的内容)并且我无法解析它!
从我的服务:
<span ng-bind-html="$ctrl.app.publishedDate" class="ng-binding">11/20/2017</span>
PSB a screenshot
答案 0 :(得分:0)
您是否尝试在http post post上添加{observe:'response'}
属性?
this.http.post(this.url + "resource", JSON.stringify(resource),{observe:'response'})
//.map(this.extractData)
.map(res => this.errorStatus = res)
.catch(this.handleObservableError)
.subscribe();
它将为您提供一个包含标题和响应状态的完整Http响应对象
答案 1 :(得分:0)
这就是我最后解决的问题:
addDbResource(resource) {
//console.log(JSON.stringify(resource));
return this.http.post(this.url + "resource", JSON.stringify(resource), this.options)
.catch(this.handleObservableError);
//.subscribe();
//More flexible approach
/*return this.http.post(this.url + "resource", JSON.stringify(resource), this.options)
.subscribe(response => {
if (response.ok) { // <== CHECK Response status
//this.data = response.json();
} else {
// handle bad request
}
},
error => {
this.errorMessage = <any>error;
document.getElementById("alert").innerHTML = error.json().message;
console.log("-> " + error.status + " : " + this.errorMessage);
});*/
}
并从调用addResource方法的组件:
response.subscribe(res => {
if (res.status == 200) window.location.href = "app/components/resourses-list";
});
我意识到我无法从订阅中保存我想要的数据,然后在外面使用它。 逻辑必须在订阅中。