-1

I have installed barryvdh/laravel-cors as explained on the readme file in the github repository. I still have a No 'Access-Control-Allow-Origin' header is present on the requested resource error. I am using vue, axios and laravel 5.8.8

Installed barryvdh/laravel-cors as well as added headers in my api.php file

My cors.php file looks like this

'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedOriginsPatterns' => [],
    'allowedHeaders' => ['Content-Type', 'X-Requested-With', 
                    'Origin','Authorization'],
    'allowedMethods' => ['*'],
    'exposedHeaders' => [],
    'maxAge' => 0,

In debugger tools i get the following

GENERAL
Request URL:http://localhost:8000/api/entriesoff
Request Method:OPTIONS
Status Code:200 OK
Remote Address:127.0.0.1:8000
Referrer Policy:no-referrer-when-downgrade
Response Headers
Allow:GET, POST, HEAD, CONNECT, PUT, DELETE, OPTIONS, PROPFIND, MKCOL
DAV:1
Request Headers
Provisional headers are shown
Access-Control-Request-Headers:x-csrf-token,x-requested-with
Access-Control-Request-Method:GET
Origin:http://127.0.0.1:8000
Referer:http://127.0.0.1:8000/
Noela
  • 17
  • 2
  • `PROPFIND, MKCOL` are non-standard. Is there anything intercepting the response that may modify it after it's been sent? – apokryfos May 21 '19 at 10:23
  • The debugger tools are showing the response to the **preflight** OPTIONS request, but the error message says "*on the requested resource*" … so you should be looking at the response the subsequent GET request instead. – Quentin May 21 '19 at 10:53
  • The subsequent get request is not sent. – Noela May 21 '19 at 11:56
  • If you are running your server using `php artisan serve` you may have to restart it. Try running `php artisan config:clear` to see if that resolves anything. – Adam Rodriguez May 21 '19 at 18:10
  • Since you didn't mention it, did you remember to add to your middleware? `app/http/Kernel.php` this `\Barryvdh\Cors\HandleCors::class` – Noogen May 21 '19 at 22:32
  • You are using different origins. "127.0.0.1" and "localhost" are different origins. Try using "127.0.0.1" for all requests. See https://stackoverflow.com/questions/5256251/are-127-0-0-1-and-localhost-considered-as-two-different-domains-by-browsers – Czarek Tomczak May 22 '19 at 07:06
  • I used php artisan serve --host=127.0.0.1 --port=8000 to set my URL to 127.0.0.1 same as origin and it works. However, I would like a permanent solution, that is, not having to run the artisan command each time. The whole point of using php desktop was to be able to run the app by launched the .exe file. Any suggestions? – Noela May 22 '19 at 20:11

2 Answers2

0

I found a solution. I added index.php in my axios URL , for example http://127.0.0.1:8000/index.php/api/...

Noela
  • 17
  • 2
-2

you will use

header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

instead of barryvdh/laravel-cors

in

public/index.php 

above

require __DIR__.'/../bootstrap/autoload.php';

sample:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
require __DIR__.'/../bootstrap/autoload.php';
Amin Jafari
  • 389
  • 4
  • 13