var chApiKey = "{{MYAPIKEY}}:";
var company_number = "09963675";
$.ajax({
async: false,
url: "https://api.companieshouse.gov.uk/company/",
type: "GET",
crossDomain: true,
contentType: "application/json",
data: company_number,
dataType: 'jsonp',
success: function(data) {
//Response text
alert(data);
},
beforeSend: function(xhr, settings) {
console.log("dataType: 'jsonp' :", settings.url)
xhr.withCredentials = true;
xhr.setRequestHeader("Authorization", make_base_auth(encodeURIComponent(chApiKey)))
// xhr.setRequestHeader ("Authorization", make_base_auth(chApiKey))
},
error: function() {
//Gat failure
//console.log("error");
}
});
function make_base_auth(user) {
// var user = user + ":"
//console.log("the api is: " + user)
var hash = btoa(user);
return "Basic " + hash;
// console.log("Basic " + hash + ":");
}
$.ajax({
async: false,
url: "https://api.companieshouse.gov.uk/company/",
type: "GET",
crossDomain: true,
contentType: "application/json",
data: company_number,
success: function(data) {
//Response text
alert(data);
},
beforeSend: function(xhr, settings) {
console.log("dataType: 'jsonp' removed :", settings.url)
xhr.withCredentials = true;
xhr.setRequestHeader("Authorization", make_base_auth(encodeURIComponent(chApiKey)))
// xhr.setRequestHeader ("Authorization", make_base_auth(chApiKey))
},
error: function() {
//Gat failure
//console.log("error");
}
});
$.ajax({
async: false,
url: "https://api.companieshouse.gov.uk/company/" + company_number,
type: "GET",
crossDomain: true,
contentType: "application/json",
success: function(data) {
//Response text
alert(data);
},
beforeSend: function(xhr, settings) {
console.log("company number added in url :", settings.url)
xhr.withCredentials = true;
xhr.setRequestHeader("Authorization", make_base_auth(encodeURIComponent(chApiKey)))
// xhr.setRequestHeader ("Authorization", make_base_auth(chApiKey))
},
error: function() {
//Gat failure
//console.log("error");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>