Best Partner
Best Partner

Reputation: 397

How to solve the CORS error in Laravel + Nuxt.js

I am installing the old project, which is made by Nuxt.js (frontend) and Laravel (backend) on my local.

Access to XMLHttpRequest at 'http://localhost:8000/api/user' from origin 'http://127.0.0.1:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

This is the issue that I faced to run the project.

Frontend configuration for the request(nuxt nuxt.config.js).

user: {
    url: '/api/user',
    method: 'get',
    propertyName: false,
    withCredentials: true,
    headers: {
        X-Requested-With': 'XMLHttpRequest',
        Content-Type': 'application/json'
    }
}

Backend configuration (Laravel config/cors.php)

'paths' => ['api/*'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,

I tried to find the solution by googling but I didn't find the correct solution.

withCredetials is true

CORS: credentials mode is 'include'

The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'

So, I want someone to help me in this field.

Upvotes: 0

Views: 1907

Answers (1)

Abolfazl Mohajeri
Abolfazl Mohajeri

Reputation: 1987

Try this way in your laravel app:

php artisan serve --host YOUR_LOCAL_IP

YOUR_LOCAL_IP is your ip that can access with ifconfig command in linux and ipconfig in windows.

Finally you should request to YOUR_LOCAL_IP:PORT/api

Upvotes: 0

Related Questions