Request with fetch() is not authorized

Hi,

Sorry if this is related to other questions that have been answered. I’m new to this and I’m trying to do a simple fetch to pull data from the API and then console.log the results. This is my code:

  var key = 'API_KEY:';
  var headers = new Headers({
      'Authorization': 'Basic ' + btoa(key),
      'Content-Type': 'text/json',
  });
    
  var obj = {
    mode: 'no-cors',
    method: 'GET',
    headers: headers
  }

  fetch('https://api.companieshouse.gov.uk/search/companies', obj).then(function(res) {
    return res.json();
  }).then(function(res) {
    console.log(res);
  }).catch(function(err) {
    console.log(err);
  });

I’ve mapped localhost to a domain like: domain.com and then assign http://domain.com to the Javascript domains field on the Companies House website but I still get 401 (Unauthorized) error. Was wondering if someone can help me understand what I’m doing wrong. Thanks in advance!

Resolved. After researching on the internet, I’ve found out that, using fetch() with no-cors mode is not sending the authorised header and that’s why I was receiving the response.