R
R
Roman2019-11-18 16:24:46
CORS
Roman, 2019-11-18 16:24:46

How to force Laravel to send correct headers for CORS?

I tried https://github.com/barryvdh/laravel-cors/ - but it somehow works strangely, sometimes it works, sometimes it doesn't work, and I can't grasp what it depends on, I crashed it for two hours.
Tried to send headers directly. For example:

return response()->json(['response' => 'no query given'])
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');

but it still writes to the browser console
No 'Access-Control-Allow-Origin' header

Despite the fact that Postman sees and shows these headers. Chrome doesn't see them. How can this be?
In general, some kind of garbage :(
Is there a 100% method to somehow do this and already forget about this CORS as a bad dream? :D
Thanks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2019-11-18
@Sanasol

barryvdh/laravel-cors solves this 100% out of the box, you don't even need to configure anything.
If it doesn't help, then you did something wrong.
the full text of the error?
What version of laravel?
ServiceProvider registered if the version is old?
middleware from laravel-cors added to your routes?
What is laravel-cors config?
Did you create it at all? Did they change?

A
Anton Anton, 2019-11-18
@Fragster

These headers must be sent in response to the options method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question