在AWS Lambda函数中添加CORS头使其允许跨域请求。
示例代码如下:
exports.handler = async (event, context) => {
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*', // 允许所有源访问
'Access-Control-Allow-Headers': 'Content-Type', // 允许自定义请求头
},
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};