error
http://localhost:8000/sanctum/csrf-cookie’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource
nuxt axios config
import axios from “axios”
export default defineNuxtPlugin((NuxtApp) => {
axios.defaults.withCredentials = true;
axios.defaults.withXSRFToken = true;
axios.defaults.baseURL = 'http://localhost:8000'
return {
provide: {
axios: axios
},
}
})
Laravel Cors
'paths' => [
'api/*',
'/login',
'/register',
'/logout',
'/sanctum/csrf-cookie'
],
'allowed_methods' => ['*'],
'allowed_origins' => [env('FRONTEND_URL', 'http://localhost:3000')],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
];
the translator such settings do not work gives the above error
I tried the proxy and nothing worked (same error
const axios = require(‘axios’);
module.exports = async function (req, res) {
const { data, headers } = await axios({
baseURL: ‘http://localhost:8000’,
url: req.url,
method: req.method,
headers: {
…req.headers,
‘Access-Control-Allow-Origin’: ‘http://localhost:3000’, // Добавляем нужные заголовки CORS
‘Access-Control-Allow-Methods’: ‘GET, POST, PUT, DELETE’,
‘Access-Control-Allow-Headers’: ‘Content-Type’,
},
data: req.body || null,
});
Object.keys(headers).forEach((key) => {
res.setHeader(key, headers[key]);
});
res.end(JSON.stringify(data));
};
nuxt.config.ts
serverMiddleware: [
{
path: ‘/api’,
handler: ‘~/server-middleware/api-proxy.js’,
},
],
Adrian Cerush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.