简体   繁体   中英

CORS issue with AWS API Gateway in angular 4

I have tried by adding interceptor but its not working

import {Injectable } from '@angular/core';
import {
    HttpRequest,
    HttpHandler,
    HttpEvent,
    HttpInterceptor
} from '@angular/common/http';
//import { AuthService } from '../services/auth.service';
import { Observable } from 'rxjs/Observable';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import 'rxjs/add/operator/catch';


@Injectable()
export class TokenInterceptor implements HttpInterceptor
{
    constructor(private http: HttpClient) 
    {
        console.log("interceptor is getting called");
    }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

      console.log(JSON.stringify(request));

       request = request.clone({

        setHeaders: {
            'Content-Type':  'application/json',
            'Authorization': 'my-auth-token',
        } 
      });
      return next.handle(request).catch((error ,caught )=>{
        //intercept the respons error and displace it to the console
        console.log("Error Occurred");
        console.log(error);
        //return the error to the method that called it
        return Observable.throw(error);``
        }) as any; 
    }
}

Make sure your server allows CORS with something like this:

"headers": { "Access-Control-Allow-Origin": "*", "Content-Type": "application/json" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM