Companies House API jquery ajax返回401

时间:2018-11-16 11:33:34

标签: javascript jquery ajax

我正在尝试从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

2 个答案:

答案 0 :(得分:0)

可能您获得的URL与您期望的不同(使用?,因为这是将data附加到URL的GET请求中的方式。)

您需要将company_number添加到URL(并适当删除dataType: 'jsonp'):

url: "https://api.companieshouse.gov.uk/company/" + company_number,

下面的代码段在3种情况下显示了您的请求的网址:

  1. 您当前的代码
  2. dataType: 'jsonp'删除后的代码
  3. 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,