2017-09-29 12:02
浏览 136

Angular 4前端中的CORS问题与运行go API后端的Google Cloud Endpoints

I am unable to call API running in the google cloud behind cloud endpoints from the Angular 4 front end. I am getting following errors:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 403.

I am running swagger too on the local machine and making successful API calls from swagger.
My various settings are as follows:

OpenAPI(Swagger Specification):

  - name:
    allowCors: true

GO Server CORS Settings:   I am using "" library

c := cors.New(cors.Options{
        AllowedOrigins:   []string{"*"},
        AllowedHeaders:   []string{"*"},
        AllowedMethods:   []string{"GET", "PUT", "POST", "HEAD", "POST"},
        AllowCredentials: true,

Angular 4 App:

const payloadHeaders = new Headers({'Access-Control-Allow-Origin' : 'http://localhost:4200','authorization':'Bearer *********'});
    var url = '' + username;
    .map((response) => {
      return response.json();

The only difference between swagger and angular might be how they make http request...though I am not sure. Swagger origin is '' whereas Angular is 'localhost:4200'

Does anyone know how I can overcome this CORS issue?

The Stackdriver log throws: Endpoints management skipped for an unrecognized >HTTP call: OPTIONS /users/user? But I think I have taken care of that in GO server itself. So it is a bit >confusing.


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题