M
M
Minat0_2020-11-07 02:27:00
Laravel
Minat0_, 2020-11-07 02:27:00

Why is the CORS header missing in Laravel when sending an AJAX request?

Hey!
Sending an AJAX request like this:

$.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': "{{ csrf_token() }}"
                }
            });

            $.ajax({
                url: "https://www.example.ru/confirm",
                type: "post",
                data: {type: "Customer", order_id: order_id},
                dataType: "json",
                error: function (err) {
                    console.log(err);
                }
            });
        });


An error occurs:
Request from foreign source blocked: The single origin policy prohibits reading the remote resource at https://example.ru/confirm . (Reason: missing CORS header "Access-Control-Allow-Origin").


It costs Laravel v.7.19.1, installed the barryvdh/laravel-cors package in addition, but it did not help.
And how would I understand that there is no header in the response, but I don’t know how to put it ...
Also, I tried to make my own global middleware, it didn’t help either.

Any ideas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Remizov, 2020-11-07
@zhenyavka

Use the ready-made middleware - \Fruitcake\Cors\HandleCors::class + by specifying the desired route in the config, as described here https://github.com/fruitcake/laravel-cors in the Global Usage section.
If it starts up, then you can figure out what was wrong with your middleware.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question