2017-10-08 152 views
1

我正在尝试从一个angularjs应用程序向我使用无服务器设置的lambda函数执行http请求。无服务器AWS Lambda CORS错误

这里是我的serverless.yaml功能

functions: 
    createcustomer: 
    handler: handler.createcustomer 
    events: 
     - http: post /createcustomer 
     cors: true 

客户创造功能

module.exports.createcustomer = (event, context, callback) => { 

    let customer = JSON.parse(event.body).customer; 

    service.create(customer, function(result) { 
     let response = { 
      statusCode: 200, 
      headers: { 
       "Access-Control-Allow-Credentials": true, 
       "Access-Control-Allow-Origin": "*", 
       "Content-Type": "application/json", 
      }, 
      body: JSON.stringify({ 
       result: 'Created Customer Successfully', 
       message: 'The account has been created!', 
       type: 'success', 
       customer: result 
      }) 
     }; 
     callback(null, response); 
    }); 
}; 

从我AngularJS程序,我这样称呼它

app.factory('MyFactory', ['$http', function($http) { 
    return { 
     CreateCustomer: function(customer) {$http.post('<apipath>/createcustomer', {customer:customer})} 
    } 
}]); 

不过,我不断收到此错误:

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:5000 ' is therefore not allowed access. The response had HTTP status code 403.

我试图在POST方法的API网关中启用CORS,但是这并没有改变结果。

我也试过在YAML文件中明确

functions: 
    createcustomer: 
    handler: handler.createcustomer 
    events: 
     - http: post /createcustomer 
     cors: 
      origin: '*' 

仍然没有运气设定CORS。

有没有人知道我在做什么错在这里?

一件奇怪的事是我可以通过PostMan让帖子工作得很好,但是如果我通过我的应用程序尝试它,它会中断。

感谢

UPDATE

enter image description here

当我做serverless deploy它显示在AWS像上面的图片和方法看起来像这样

enter image description here

正如我所说的之前,我试图直接从API网关控制台启用CORS,但是当我尝试调用该方法时,这并不是什么区别。

+0

尝试在Lambda响应中添加Access-Control-Allow-Headers标头。 –

+0

@KaustubhKhare尝试过,仍然没有运气。 – DevShadow

+0

根据您发布的内容来看是否正确。我将检查API网关控制台和Lambda控制台,以检查这些设置是否真正反映并正确部署。 – dashmug

回答

1

你有更新的截图显示,OPTIONS方法不设置任何这些资源。在控制台中为API网关资源启用CORS时,AWS会自动设置它。

当您为资源启用CORS时,您可以在AWS控制台中看到此情况,但当然,您的serverless deploy正在覆盖此配置。

要在无服务器部署中在AWS中正确配置/createcustomer资源,您可以重写这部分无服务器。YAML:

events: 
    - http: post /createcustomer 
    cors: true 

看起来像这样:

events: 
    - http: 
     path: /createcustomer 
     method: post 
     cors: true 

我不是框架的.yml语法方面的专家,所以我无法确切地解释这是为什么。

尽管如此,我已经证实,像这样的文件:

functions: 
    deletecustomer: 
    handler: handler.deletecustomer 
    events: 
     - http: 
      path: /deletecustomer 
      method: post 
      cors: true 
    createcustomer: 
    handler: handler.createcustomer 
    events: 
     - http: post /createcustomer 
     cors: true 

将在AWS API网关,一个正确配置CORS创建两个资源,一人失踪OPTIONS方法:

Two resources, one correct and one missing OPTIONS

+0

这就像一个魅力,谢谢! – DevShadow

1

这是可以帮助的配置。请注意,在允许CORS的起源方面具体一直是安全的。所以最好将源端锁定到localhost:{port-number}。另外,您还可以在CORS设置上启用凭据。请看下面的无服务器的配置为例:

cors: 
 
    origin: 'http://localhost:4200' 
 
    headers: 
 
    - Content-Type 
 
    - X-Amz-Date 
 
    - Authorization 
 
    - X-Api-Key 
 
    - X-Amz-Security-Token 
 
    - X-Amz-User-Agent 
 
    allowCredentials: true