![](/img/trans.png)
[英]Setting default requests header from ionic storage using http interceptor - Angular 5 - Ionic 3
[英]HTTP interceptor causing CORS issue using ionic/angular
在這里,我也使用 ionic 用於網絡和移動設備,並且我在集成它時使用 https url 我遇到了 cors 問題。后來當我在刪除http 攔截器后檢查 cors 問題已解決但我如何解決這個問題下面是我的代碼
問題:在使用攔截器時,我遇到了 CORS 問題,如果我刪除了攔截器,它就可以正常工作。
export class HttpConfigInterceptor implements HttpInterceptor {
token:any;
constructor(public storage: Storage ) {
this.storage.get('Token').then(data => {
this.token = data;
});
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
//Authentication by setting header with token value
if (this.token) {
request = request.clone({
setHeaders: {
Authorization: this.token
}
});
}
if (!request.headers.has('Content-Type')) {
request = request.clone({
setHeaders: {
'content-type': 'application/json'
}
});
}
request = request.clone({
headers: request.headers.set('Accept', 'application/json')
});
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
}
return event;
}),
catchError((error: HttpErrorResponse) => {
console.error(error);
return throwError(error);
}));
}
}
我在 cors 給出 200 並給出 text/html 時遇到問題的請求
在我看來,這一切聽起來都像是您的問題是添加Authorization
標頭會使GET
請求“不安全”,因此您的客戶端在GET
之前發出OPTIONS
預檢請求,並且您的 API 網關未配置為處理OPTIONS
方法請求。 您可能需要在網關中添加通用OPTIONS
方法處理程序,並確保 CORS 允許OPTIONS
方法。
無論如何,這個問題幾乎肯定是在服務器端,需要你的服務器配置來提供如何解決這個問題的詳細解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.