Hi,
Can I please get help with the following code, which is resulting in 401 code.
$(document).ready(function() {
console.log("api");
xhr = new XMLHttpRequest();
$(document).ready(function() {
$.ajax({
url: 'https://api.companieshouse.gov.uk/company/05961102',
type: 'GET',
datatype: 'json',
success: function() { alert('Success'); },
error: function() { alert('Failed!' + xhr.readyStatus ); },
beforeSend: function (xhr) {
xhr.withCredentials = true;
var key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:';
key = btoa(key);
xhr.setRequestHeader('Authorization', 'Basic ' + encodeURIComponent(key));
alert(encodeURIComponent(key));
},
});
});
});
When running this in developer tool I am getting 401 authorised error. My understanding is that it might something to do with encoding of the key but not sure.
Thanks in advance.