我正在尝试从gov.uk公司内部API返回json,但是却收到401
无法加载资源:服务器的响应状态为401(未经授权)
我正在使用ajax()方法使用jQuery,我的代码如下:
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) {
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 + ":");
}
gov.uk上的说明允许您使用有效的API密钥进行测试。我还使用终端中的curl命令返回了json
curl -u{{MYAPIKEY}}: https://api.companieshouse.gov.uk/company/09963675
答案 0 :(得分:0)
可能您获得的URL与您期望的不同(使用?
,因为这是将data
附加到URL的GET请求中的方式。)
您需要将company_number
添加到URL(并适当删除dataType: 'jsonp'
):
url: "https://api.companieshouse.gov.uk/company/" + company_number,
下面的代码段在3种情况下显示了您的请求的网址:
dataType: 'jsonp'
删除后的代码company_number
直接添加到url
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>
答案 1 :(得分:0)
在https://developer.companieshouse.gov.uk应用设置中,标记为 JavaScript域的字段必须
:对于本地安装,请更新hosts文件并更新应用程序设置中的JavaScripts域。
我犯了一个错误:没有在此字段中包含:3000。因此我的本地域是http://ch-test.com,但应该是http://ch-test.com:3000,因为我的http服务器在端口3000上运行。
对其他人的反馈也发生了变化
dataType:“ jsonp”, => dataType:“ json”,
异步:是的, => 异步:false,