我有一个自定义http服务,它覆盖默认的角度2 http服务。此服务设置自定义默认选项,并在请求开始时设置加载器,并在请求完成时隐藏加载器。Angular 2 http捕获可观察问题
我也想添加,如果从服务器收到未授权错误(401,403)的情况下,将用户重定向到登录页面的功能。例如下面是我的HTTP方法
get(url: string, options?: RequestOptionsArgs): Observable<any> {
this.setLoadingStatus();
options = this.prepareOptions(options);
let that = this;
return super.get(url, options)
.map(res => {
this.clearLoadingStatus();
return res.json();
})
.catch(function (err) {
//redirects user to login and display alert
that._gs.alertUnauthorized();
return Observable.empty();
});
}
但是,当测试在IE浏览器做了保护的页面的请求,在未经授权的请求的情况下返回401,这行的也重定向到登录页面,但下面的错误控制台显示:
GET http://localhost:49547/api/protected/ 401 (Unauthorized)
EXCEPTION: Observable_1.Observable.empty is not a function
ORIGINAL STACKTRACE:
TypeError: Observable_1.Observable.empty is not a function
任何人都可以指导吗?
尝试'super.get()图((RES)=> {},(ERR)=> {});'的语法? –