Hi,
Am trying to retrieve company number data via XMLHttpRequest() for a coding project. Am new to coding. Trying to create a webpage that on input of a company name retrieves and renders a company number. Below my code.
2 errors cropping up:
- Access to XMLHttpRequest at ‘https://api.companieshouse.gov.uk/companies’ from origin ‘https://8000-c6e30799-0203-472e-8101-ed7491e3ab02.ws-eu01.gitpod.io’ 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.
2.Failed to load resource: net::ERR_FAILED
function getData(cb) {
var request = new XMLHttpRequest();
var username = “API_Key”;
var password = “”;
request.open(‘GET’, ‘https://api.companieshouse.gov.uk/companies’, true);
request.setRequestHeader(“Authorization”, "Basic " + btoa(username + “:” + password));
request.setRequestHeader(“Content-Type”, “application/json”);
request.setRequestHeader(“Accept”, “application/json”);
request.withCredentials = true;
request.send();
console.log(request);
}
function writeToDocument(company_number) {
getData(company_number, function(data) {
document.getElementById(“data”).innerHTML = data;
});
}
Can anyone help me with this please?