E
E
Emil Rakhmatullin2020-11-26 17:41:36
CORS
Emil Rakhmatullin, 2020-11-26 17:41:36

How to set up cross domain requests in Laravel + JS?

There is a widget that is embedded on the site foo.ru and makes a request to its server on bar.com :"

function getDataWidget(config) {
    let xhr = new XMLHttpRequest()
    xhr.open('GET', `https://bar.com/widget/${config.key}`)
    xhr.responseType = 'json'

    xhr.onreadystatechange = function() { 
        if (xhr.readyState !== 4) return; 
        if (xhr.status === 200)
        {
            return initWidget(config, xhr.response)
        }
        else
        {
            console.log('HTTP error', xhr.status, xhr.statusText);
        }
    };

    xhr.send()
}

to display data from this server to the foo.ru site .
I get the usual error:

Access to XMLHttpRequest at ' https://bar.com/widget/b1Mz3t9XVXefDGyk ' from origin ' https://foo.ru ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

HTTP error 0

On other sites they write incomprehensibly. Can you please explain how to make the widget able to receive data from its server for the site it connects to?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question