【发布时间】:2018-12-06 13:31:49
【问题描述】:
如何使用 HttpInterceptor 中断 httpRequest 并返回数据(在本例中为 json)?
在我用于添加 http 标头的代码下方,如果调试为真,我想中断 http 请求并返回一个 JSON。
export class PostRequestInterceptor implements HttpInterceptor {
//FakeResponse is a class which return JSON data passing type
fakeResponse:FakeResponse = new FakeResponse();
debug:boolean = false;
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if(this.debug){
let jsonFakeResponse = this.fakeResponse.faker("ticket");
// return the json jsonFakeResponse
}else{
const changedReq = req.clone({headers: req.headers.set('Content-Type', 'application/x-www-form-urlencoded'),withCredentials:true});
return next.handle(changedReq);
}
}
}
我知道我应该返回一个 observable (ofc) 但是如何返回它已经解决了?
谢谢!
【问题讨论】:
标签: angular typescript ionic3 observable angular-http-interceptors