我有一个服务类,它对REST服务器进行HTTP POST调用。服务器使用HTTP状态代码响应错误,如果资源不存在则为404,如果用户无权访问此资源,则为401,依此类推。
@Injectable()
export class BacklogService extends RestService {
constructor(
private http: Http,
private authenticationService: AuthenticationService) {
super();
}
public create(
projectId: number,
type: string,
description: string,
isReady: boolean,
estimation: string | null) {
const headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Authorization', 'Bearer ' + this.authenticationService.getToken());
const options = new RequestOptions({headers: headers});
return this.http.post(this.getRequestUrl('Project/' + projectId + '/Backlog'), {type: type, description:description, isReady: isReady, estimation: estimation}, options).subscribe(
(response: Response) => {
return response.json();
},
(error: Error) => {
// error.status doesn't work
console.log("error=", error);
}
);
}
}
我试图在错误情况下捕获这些HTTP状态代码。如果我输出错误变量,它包含我期望的正确数据,但我无权访问此对象及其键。访问error.status
不起作用。
如何解决我的问题?