How to get around the cors restriction on ajax POST method?

Are obtained from third-party API data via a POST request
Address api substituted the fictional
Server access no.

btn.addEventListener('click', function(){

 let xhr = new XMLHttpRequest();
 xhr.open('post', 'https://myapi.test.com');
 xhr.setRequestHeader('Content-type', 'application/json');
xhr.send();
 xhr.addEventListener('readystatechange', function(){
 if(xhr.readyState == 4 && xhr.status == 200){
 let data = JSON.parse(xhr.response);
console.log(data);
 // alert(data.title);
//alert(data.id);
}
});

});

As a result of performing error
script.js:11 POST https://myapi.../test 522
(anonymous) @ script.js:11
testajax/:1 XMLHttpRequest Access to at 'https://myapi.test.com' from origin 'testajax' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Using Postman to output the result as a JSON object
Postman - headers - Content-Type application/json
Authorization is missing
March 19th 20 at 09:16
2 answers
March 19th 20 at 09:18
Solution
The task is solved through the creation of a private server in nodejs+express+Biblioteka require
The server code
app.get('/primer', function(req,res){
 request.post({url:'https://mysite.com/test', form: {key:'value'}}, function(err,httpResponse,body){
console.log(body);
Res. json(JSON.parse(body));
});

});


The client part
let btn = document.getElementById('test');
let res = document.getElementById('res');

btn.addEventListener('click', function(){

 let xhr = new XMLHttpRequest();
 xhr.open('GET', 'http://localhost:3000/primer');
xhr.send();

 xhr.addEventListener('readystatechange', function(){
 if(xhr.readyState == 4 && xhr.status == 200){
 let data = JSON.parse(xhr.response);
console.log(data);
 Res. innerHTML = data.token;
}
});
});

And can someone come in handy.
March 19th 20 at 09:20

Find more questions by tags JavaScript