src/app/api/hello.ts
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
export async function OPTIONS(request: Request) {
return new Response(null, {
status: 204,
headers: corsHeaders,
});
}
export async function POST(request: Request) {
return new Response(
JSON.stringify({
message: 'hello',
}),
{
headers: corsHeaders,
}
);
}
跨域请求会发送 OPTIONS 预检请求,将两次请求都允许请求头即可