如何使用httpclient截取有角度的jsonp请求 [英] how to intercept angular jsonp requests using httpclient
本文介绍了如何使用httpclient截取有角度的jsonp请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用于HTTP get/post请求的拦截器.效果很好.
I have an Interceptor for http get/post requests. It works very well.
不幸的是,它不适用于我的jsonp请求/它们没有被捕获.
Unfortunately it does not work for my jsonp requests / they are not caught.
因此,我想创建另一个实现JsonpInterceptor的Interceptor.
Therefore I wanted to create another Interceptor that implements JsonpInterceptor.
实际上,它也不起作用.
In fact, it does not work either.
我到目前为止所做的:
AppModule:
AppModule:
[{ provide: HTTP_INTERCEPTORS, useClass: MyJsonpInterceptor, multi: true }],
MyJsonpInterceptor:
MyJsonpInterceptor:
@Injectable()
export class MyJsonpInterceptor implements JsonpInterceptor {
constructor(jsonp: JsonpClientBackend) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log("intercept");
}
}
有人知道,如何使用新的httpclient拦截角度为4/5的jsonp请求吗?
Does someone know, how to intercept jsonp requests in angular 4/5 with the new httpclient?
推荐答案
您必须在 HttpClientJsonpModule
import { NgModule, Injectable } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {
HttpClient,
HTTP_INTERCEPTORS,
HttpEvent,
HttpRequest,
HttpHandler,
HttpClientModule,
HttpClientJsonpModule
} from '@angular/common/http'
import { Observable } from 'rxjs'
import { AppComponent } from './app.component';
@Injectable()
export class CustomInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req);
}
}
@NgModule({
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: CustomInterceptor, multi: true}
]
})
export class JsonpInterceptingModule {}
@NgModule({
imports: [
BrowserModule,
JsonpInterceptingModule, // Must be before the HttpClientJsonpModule
HttpClientModule,
HttpClientJsonpModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
providers: [
{ provide: HTTP_INTERCEPTORS, multi: true, useClass: CustomInterceptor }
]
})
export class AppModule {
constructor(http: HttpClient){
http.jsonp('https://archive.org/index.php?output=json', 'callback')
.subscribe(response => console.log(response));
}
}
这篇关于如何使用httpclient截取有角度的jsonp请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文