Continuously getting {“errors”:[{“error”:“invalid-authorization-header”,“type”:“ch:service”}]} when using Jqeury Ajax request.
My code here: I am sending the api key in a var called chApiKey
function lookUpCompanyHouseById(company_number){
$.ajax({
url: "https://api.companieshouse.gov.uk/company/company_number/",
type: "GET",
crossDomain: true,
contentType: "application/json",
data: company_number,
dataType:'json',
success: function(data){
//Response text
alert(data);
},
beforeSend: function (xhr) {
xhr.withCredentials = true;
xhr.setRequestHeader ("Authorization", make_base_auth(encodeURIComponent(chApiKey)))
},
error:function(){
//Gat failure
alert("failure");
}
});
}
function make_base_auth(user) {
var tok = user;
var hash = btoa(tok);
return "Basic " + hash;
}