为什么 AWS Lambda,API 网关返回 CORS 错误
Why is AWS Lambda, API Gateway returning a CORS error
我知道网络上的很多帖子都提到了这个问题,我想我已经尝试了所有这些,但我的本地 React 应用程序仍然收到 403 CORS 错误。
部分来自 Dev Tools 的 Headers:
#GENERAL:
Request URL: https://<myGatewayApiUrl>.amazonaws.com/dev/api/byid/1/129
Request Method: OPTIONS
Status Code: 403
#RESPONSE HEADERS
access-control-allow-headers: Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token
access-control-allow-methods: GET,OPTIONS
access-control-allow-origin: *
content-length: 42
content-type: application/json
我一直在 API 网关中工作,设置 Enable CORS
,但我收到一个获取方法的错误 Add Access-Control-Allow-Origin Integration Response Header Mapping to GET method
-> invalid response status code specified
- 但是OPTIONS headers 已设置,GET header Access-Control-Allow-Origin
已设置。
我正在使用 express 和 cors 包,这是我的 API index.js 文件中的一个片段:
const app = express();
app.use(cors());
app.options('*', cors());
这是来自 React 应用程序的请求代码:
export const getRecordById = async (userId, id, token) => {
try {
const response = await axios.get(
process.env.REACT_APP_API_URL + `/byid/${userId}/${id}`,
{
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
}
);
return response.data;
} catch (error) {
console.log('ERROR', error);
return error;
}
};
这是我来自 Lambda 的响应代码 API:
getById: asyncHandler(async (req, res, next) => {
const { user, id } = req.params;
const result = await recordsService.getRecordById(user, id);
res.set({
'content-type': 'application/json',
'Access-Control-Allow-Origin': '*',
});
if (!result) {
res.status(400).json({
error: true,
message: 'get record by ID action failed',
data: {},
});
}
res.status(200).json({
error: false,
message: 'successful record retrieval',
data: {
record: result,
},
});
}),
此外,我的 serverless.yml 文件 http 事件设置如下:(据我了解 cors: true
应该处理预检请求)
- http:
path: /api/records/byid/{user}/{id}
method: GET
cors: true
我花了太多时间试图解决这个问题。它一定是简单而愚蠢的东西,我是否正确使用 res.set()
?一切看起来都是正确的,我知道我遗漏了一些东西。谢谢
API 默认情况下未找到 URL 时,网关将拒绝调用并返回 CORS 错误。
Axios 似乎缺少请求 URL 中的 /records
位。
我知道网络上的很多帖子都提到了这个问题,我想我已经尝试了所有这些,但我的本地 React 应用程序仍然收到 403 CORS 错误。
部分来自 Dev Tools 的 Headers:
#GENERAL:
Request URL: https://<myGatewayApiUrl>.amazonaws.com/dev/api/byid/1/129
Request Method: OPTIONS
Status Code: 403
#RESPONSE HEADERS
access-control-allow-headers: Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token
access-control-allow-methods: GET,OPTIONS
access-control-allow-origin: *
content-length: 42
content-type: application/json
我一直在 API 网关中工作,设置 Enable CORS
,但我收到一个获取方法的错误 Add Access-Control-Allow-Origin Integration Response Header Mapping to GET method
-> invalid response status code specified
- 但是OPTIONS headers 已设置,GET header Access-Control-Allow-Origin
已设置。
我正在使用 express 和 cors 包,这是我的 API index.js 文件中的一个片段:
const app = express();
app.use(cors());
app.options('*', cors());
这是来自 React 应用程序的请求代码:
export const getRecordById = async (userId, id, token) => {
try {
const response = await axios.get(
process.env.REACT_APP_API_URL + `/byid/${userId}/${id}`,
{
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
}
);
return response.data;
} catch (error) {
console.log('ERROR', error);
return error;
}
};
这是我来自 Lambda 的响应代码 API:
getById: asyncHandler(async (req, res, next) => {
const { user, id } = req.params;
const result = await recordsService.getRecordById(user, id);
res.set({
'content-type': 'application/json',
'Access-Control-Allow-Origin': '*',
});
if (!result) {
res.status(400).json({
error: true,
message: 'get record by ID action failed',
data: {},
});
}
res.status(200).json({
error: false,
message: 'successful record retrieval',
data: {
record: result,
},
});
}),
此外,我的 serverless.yml 文件 http 事件设置如下:(据我了解 cors: true
应该处理预检请求)
- http:
path: /api/records/byid/{user}/{id}
method: GET
cors: true
我花了太多时间试图解决这个问题。它一定是简单而愚蠢的东西,我是否正确使用 res.set()
?一切看起来都是正确的,我知道我遗漏了一些东西。谢谢
API 默认情况下未找到 URL 时,网关将拒绝调用并返回 CORS 错误。
Axios 似乎缺少请求 URL 中的 /records
位。