This commit is contained in:
Tben 2023-08-21 09:39:44 +01:00
parent 1a16808623
commit 1f3ddaab52
2 changed files with 23 additions and 2 deletions

View File

@ -13,4 +13,17 @@ module.exports = {
}, },
], ],
}, },
async headers() {
return [
{
source: "/api/test-route",
headers: [
{ key: "Access-Control-Allow-Credentials", value: "true" },
{ key: "Access-Control-Allow-Origin", value: "*" },
{ key: "Access-Control-Allow-Methods", value: "GET,OPTIONS,PATCH,DELETE,POST,PUT" },
{ key: "Access-Control-Allow-Headers", value: "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" },
],
},
];
},
}; };

View File

@ -24,13 +24,21 @@ function runMiddleware(req: NextApiRequest, res: NextApiResponse, fn: Function)
* @type {NextApiHandler} * @type {NextApiHandler}
*/ */
export default async function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
// res.setHeader("Access-Control-Allow-Credentials", "true");
// res.setHeader("Access-Control-Allow-Origin", "*"); // res.setHeader("Access-Control-Allow-Origin", "*");
// res.setHeader("Access-Control-Allow-Methods", "*"); // res.setHeader("Access-Control-Allow-Methods", "*");
// res.setHeader("Access-Control-Allow-Headers", "*"); // res.setHeader("Access-Control-Allow-Headers", "*");
await runMiddleware(req, res, cors); res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Origin", "*");
// another common pattern
// res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
res.setHeader("Access-Control-Allow-Methods", "GET,OPTIONS,PATCH,DELETE,POST,PUT");
res.setHeader("Access-Control-Allow-Headers", "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version");
res.status(200).json({ // await runMiddleware(req, res, cors);
res.json({
title: "Hello There", title: "Hello There",
message: "General Kenobi", message: "General Kenobi",
}); });