C
C
Crash2018-05-22 14:28:37
PHP
Crash, 2018-05-22 14:28:37

How to configure server and headers for cross domain requests?

I'm trying to set up cross-domain requests via fetch.
This query runs fine and returns data:

fetch('https://www.cbr-xml-daily.ru/daily_json.js', {
    mode: 'cors'
}).then(res => {
    return res.json();
}).then(data => {
    console.log(data);
});

But this one, designed to communicate with the backend of the project, is not:
fetch('https://mysite.ru/get_some_stuff', {
    mode: 'cors'
}).then(res => {
    return res.json();
}).then(data => {
    console.log(data);
});

Returns the following error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://mysite.ru/get_some_stuff. (Reason: CORS request did not succeed).

As far as I understand at the moment, fetch by default is perfectly able to work with such requests, the problem is most likely in the headers returned by the server upon request.
Now I have these, but this did not solve the problem:
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
string15, 2018-05-22
@string15

As a temporary solution

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question