由于 CORS 对 SO 的支持,我几乎不好意思问这个问题,但我无法解决:
Access to XMLHttpRequest at 'https://a93xxxxx.execute-api.eu-west-1.amazonaws.com/dev[object%20Object]' from origin 'https://www.example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
我什至用 Amplify 发布了我的 React 项目,并尝试从真实域名中删除任何与开发环境有关的东西(运行 npm 版本 6.14.8 的 Cloud 9)
我还使用 --disable-web-security 标志对运行 Chrome 进行了测试。
我的 Lambda 函数包含以下内容(开箱即用的存根)
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
// Uncomment below to enable CORS requests
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers" : "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With",
"Access-Control-Allow-Methods" : "OPTIONS,POST,GET,PUT"
}
,
body: JSON.stringify("Hello from Lambda!")
};
return response;
};
请注意,我取消了 CORS 请求部分的注释,并且响应 statusCode 设置为 200。当从客户端发送提交表单时,我的应用程序中执行的代码:
uploadcontactusdata = async data => {
try {
console.log("Contact Us pressed")
const settings = {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
}
const fetchResponse = await API.post('econtactus', settings);
Notification({
title: 'Success',
message: 'Notification has been sent',
type: 'success'
});
}
catch (err) {
console.log("unable to send");
console.error(err)
}
}
我使用 Amplify(版本 4.41.2)创建了 API Gateway + Lambda。不知道现在还能去哪里看。任何线索将不胜感激。谢谢