Как обойти проблему с CORS при попытке загрузить данные с помощью брузерного javascript кода?
Как обойти проблему с CORS при попытке загрузить данные с помощью брузерного javascript кода? Ниже приведен рабочий код. Если поменять значение url на https://ftx.com/api/markets , то код возвращает ошибку приведенную ниже. Конечная задача с веб страницы с помощью javascript напрямую загружать данные с API бирж и отображать их.
Access to fetch at 'https://ftx.com/api/markets' from origin 'http://localhost:5500' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Рабочий код
//let url = ''https://ftx.com/api/markets''
let url = 'https://jsonplaceholder.typicode.com/posts'
fetch(url, {
mode: 'cors',
credentials: 'include',
headers: {
'Access-Control-Allow-Origin':'*',
'content-type':'application/json'
}
}).then(function (response) {
// The API call was successful!
return response.json();
}).then(function (data) {
// This is the JSON from our response
console.log(data);
}).catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});