我正在尝试从一个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
当我做serverless deploy
它显示在AWS像上面的图片和方法看起来像这样
正如我所说的之前,我试图直接从API网关控制台启用CORS,但是当我尝试调用该方法时,这并不是什么区别。
尝试在Lambda响应中添加Access-Control-Allow-Headers标头。 –
@KaustubhKhare尝试过,仍然没有运气。 – DevShadow
根据您发布的内容来看是否正确。我将检查API网关控制台和Lambda控制台,以检查这些设置是否真正反映并正确部署。 – dashmug