问题的原因是AWS Lambda函数要设置自定义域名,但未能传递原始请求中的标头,因此未能设置cookie。
可通过在Lambda函数中添加以下内容来解决此问题:
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*" // 设置允许跨域访问
},
body: JSON.stringify({message: 'Hello World'})
};
// 手动添加cookie标头
if (event.headers && event.headers.Cookie) {
response.headers['Set-Cookie'] = event.headers.Cookie;
}
return response;
当请求包含Cookie时,此代码将会将其设置在响应的“Set-Cookie” header中,从而使其在自定义域名URL上运行正常。