Loading...
Loading...
Access to fetch at 'https://api.example.com' from origin 'https://yoursite.com' has been blocked by CORS policyCORS (Cross-Origin Resource Sharing) is a browser security feature that blocks web pages from making requests to a different domain than the one that served the page. The server must explicitly allow cross-origin requests by sending the correct headers.
Use the cors npm package to add the required headers.
npm install cors
const cors = require('cors');
app.use(cors({
origin: 'https://yoursite.com',
credentials: true,
}));Set headers in your Next.js route handler.
export async function GET(request: Request) {
return new Response(JSON.stringify({ data: 'ok' }), {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
},
});
}Configure Nginx to add CORS headers to all responses.
location /api/ {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type';
}