doupeng5320 2017-09-29 12:02
浏览 137
已采纳

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):

host: MyApp.appspot.com
x-google-endpoints:
  - name: MyApp.appspot.com
    allowCors: true


GO Server CORS Settings:   I am using "github.com/rs/cors" 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 = 'https://MyApp.appspot.com/users/user?u=' + username;
    this.http.get(url,{headers:payloadHeaders}).timeout(3000)
    .map((response) => {
      return response.json();
    }).subscribe(


The only difference between swagger and angular might be how they make http request...though I am not sure. Swagger origin is '127.0.0.1:64298' 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?u=xxxx@yyyy.com But I think I have taken care of that in GO server itself. So it is a bit >confusing.

Thanks!

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 ArcGIS处理MODIS 09数据,计算EVI 像元值大小问题
      • ¥15 提个关于sql的问题
      • ¥15 Python库一直装不好
      • ¥30 在linux上调用海康SDK没有进入函数内
      • ¥15 FreeRTOS有任务卡死
      • ¥15 vue网页地址中的#问题
      • ¥20 一个js里的函数的retur值想返回另一个js的变量值,应该怎么写?
      • ¥15 登陆器jar2exe过期了怎么样重新授权
      • ¥15 我的电脑安装好的PS总是会自己消失,请问这是为什么?
      • ¥15 这个程序哪里有问题呢,显示0分