Angular 6-Ionic-使用拦截器中断http请求并返回json
如何使用HttpInterceptor中断httpRequest并返回数据(本例中为json) 在我用于添加http头的代码下面,我希望如果debug为true,则中断http请求并返回JSONAngular 6-Ionic-使用拦截器中断http请求并返回json,angular,typescript,ionic3,observable,angular-http-interceptors,Angular,Typescript,Ionic3,Observable,Angular Http Interceptors,如何使用HttpInterceptor中断httpRequest并返回数据(本例中为json) 在我用于添加http头的代码下面,我希望如果debug为true,则中断http请求并返回JSON export class PostRequestInterceptor implements HttpInterceptor { //FakeResponse is a class which return JSON data passing type fakeResponse:FakeRespons
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);
}
}
导出类PostRequestInterceptor实现HttpInterceptor{
//FakerResponse是一个返回JSON数据传递类型的类
FakerResponse:FakerResponse=新的FakerResponse();
调试:布尔值=false;
截取(req:HttpRequest,next:HttpHandler):可观察{
if(this.debug){
让jsonFakeResponse=this.fakeResponse.faker(“票证”);
//返回json jsonFakeResponse
}否则{
const changedReq=req.clone({headers:req.headers.set('Content-Type','application/x-www-form-urlencoded'),带凭证:true});
返回next.handle(changedReq);
}
}
}
我知道我应该返回一个可观察的(ofc),但如何返回已经解决的
谢谢大家! 如果有人需要,我找到了解决方案。由于Http请求等待HttpResponseEvent,因此我们必须创建一个对象HttpResponseEvent并解析它(使用承诺)
导出类PostRequestInterceptor实现HttpInterceptor{
//FakerResponse是一个返回JSON数据传递类型的类
FakerResponse:FakerResponse=新的FakerResponse();
调试:布尔值=true;
截取(req:HttpRequest,next:HttpHandler):可观察{
if(this.debug){
//从中检索jsonData
让jsonDummyData=this.fakeResponse.select(req.body.params.Stmt);
//为内容类型添加标题
const headers=新的HttpHeaders();
headers.set(“内容类型”、“应用程序/json”);
从(新承诺)返回(解决=>
//等待300毫秒以模拟internet延迟
设置超时(()=>{
解析(新的HttpResponse)({
正文:jsonDummyData,
标题:标题
}));
},300)
));
}否则{
const changedReq=req.clone({headers:req.headers.set('Content-Type','application/x-www-form-urlencoded'),带凭证:true});
返回next.handle(changedReq);
}
}
}
删除next.handle语句将中断请求
export class PostRequestInterceptor implements HttpInterceptor {
//FakeResponse is a class which return JSON data passing type
fakeResponse:FakeResponse = new FakeResponse();
debug:boolean = true;
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if(this.debug){
//retrive jsonData from
let jsonDummyData = this.fakeResponse.select(req.body.params.Stmt);
//Add header for content type
const headers = new HttpHeaders();
headers.set("Content-Type","application/json");
return from(new Promise(resolve =>
//wait 300 ms to simulate internet latency
setTimeout( () => {
resolve(new HttpResponse({
body: jsonDummyData,
headers: headers
}));
},300)
));
}else{
const changedReq = req.clone({headers: req.headers.set('Content-Type', 'application/x-www-form-urlencoded'),withCredentials:true});
return next.handle(changedReq);
}
}
}