我正在angular2中创建高级登录系统。现在我面临阻碍问题。我已经在angular2中为api通信创建了网关。这是我的代码
gateWay(Method, Url, data) {
console.log("gateWay "+Method)
this.Method = Method
this.Url = Url
this.data = data
if(Method == "get"){
return this.http.get('http://localhost:3000/' + Url, { headers: this.insertAuthToken }).map((res) => res.json())
.map((res) => res.json())
}else if(Method == "post"){
return this.http.post('http://localhost:3000/' + Url, JSON.stringify(data), { headers: this.insertAuthToken })
.map((res) => res.json());
}else if(Method == "put"){
return this.http.put('http://localhost:3000/' + Url, JSON.stringify(data), { headers: this.insertAuthToken })
.map((res) => res.json());
}else if(Method == "delete"){
return this.http.delete('http://localhost:3000/' + Url, { headers: this.insertAuthToken })
.map((res) => res.json());
}
} 如果状态200表示我能够在console.log中看到响应消息的状态代码。但如果我得到响应为403意味着我需要处理一些功能在这种情况下我面临的问题我无法处理这些功能因为我在我的组件中订阅了错误
this.httpService.gateWay('get', 'v1/users/index', undefined)
.subscribe(
data => console.log(data),
error => console.log(error),
() => console.log("finished")
)
所以请建议我一些想法触发功能,如果我得到403,否则如400意味着需要在警报中显示消息。这是我的令牌设置,这里我使用set Interval重置令牌
SetTokenDynamically(time) {
console.log("time " + time)
clearInterval(this.timer)
this.timer = setInterval(() => {
// this.http.get('https://jsonblob.com/api/jsonBlob/56d80451e4b01190df528171')
this.http.get('http://localhost:3000/v1/users/tokenUpdate', { headers: this.headerRefreshToken })
.map((res) => res.json())
.subscribe(
data => {
Cookie.setCookie('Token2', data.token + this.a)
console.log("Call " + this.a)
this.response = data;
},
error => console.log(error),
() => {
console.log(this.response)
this.insertAuthToken = new Headers({
'AuthToken': this.response.token || ""
})
Cookie.setCookie('Authorization', this.response.token)
this.SetTokenDynamically(this.response.time_expiry_sec)
}
);
}, time);
}
答案 0 :(得分:1)
这是因为在403状态代码的情况下,map
回调将不会执行,catch
回调(如果不是订阅时指定的错误回调)。
this.http.get('http://localhost:3000/' + Url, {
headers: this.insertAuthToken
})
.map((res) => res.json())
.catch((res) => { // <------
if(res.status == 403){
this.SetTokenDynamically(100);
}
});
我猜你尝试动态添加令牌,如果收到403错误并再次执行请求。
以下是一个示例:
@Injectable()
export class CustomHttp extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
(...)
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
console.log('get...');
return super.get(url, options).catch(res => {
if (res.status === 403) {
// Set token in options
return super.get(url, options);
} else {
Observable.throw(res);
}
});
}
(...)
}
如果您需要提出获取身份验证令牌的请求,则需要使用flatMap
运算符:
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.get(url, options).catch(res => {
if (res.status === 403) {
return this.getToken().flatMap(token => {
// Set token in options
this.setToken(options);
// Execute again the request
return super.get(url, options);
}
} else {
Observable.throw(res);
}
});