返回“未定义”的Ajax调用-Companies House API

时间:2018-12-12 22:21:21

标签: jquery ajax

我正在通过Companies House API获取数据,当我在页面上输出json数组时,它循环很好,但是当我在jQuery中循环它时,我得到了大约16个未定义的结果。我试图在任何地方都找到答案,但是我似乎并没有这么怀疑。我唯一的想法是16个未定义的对象被埋在另一个对象中?

任何帮助将不胜感激,代码如下:

jQuery(function($) {
  $('[data-button-search]').click(function(event) {
      var $this = $(this);
      event.preventDefault();
      var value = $('[data-company-id]').val();
      $.ajax({
          type: 'GET',
          url: "https://link.to.site.com/company/"+value+"/name",
          dataType: 'json', // ** ensure you add this line **
          success: function(data){
              $.each(data, function(){
                  $.each(this, function(key, val) {
                      console.log(val.title + " : company number : " + val.company_number);
                      $('[data-select]').append(`
                          <option value='`+key+`'>`+val.title+`</option>
                      `);
                  });
              });
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
              console.log("Error!");
          }
      });

  });
});

样本数组,这是我在搜索框中输入“ AWD”的结果:

{
  "items_per_page": 50,
  "items": [
    {
      "title": "AWD  LTD",
      "description": "11308643 - Incorporated on 13 April 2018",
      "matches": {
        "snippet": [],
        "title": [
          1,
          3
        ]
      },
      "company_type": "ltd",
      "company_status": "active",
      "links": {
        "self": "/company/11308643"
      },
      "company_number": "11308643",
      "description_identifier": [
        "incorporated-on"
      ],
      "address": {
        "locality": "London",
        "postal_code": "WC1N 3AX",
        "premises": "27 ",
        "address_line_1": "Old Gloucester Street",
        "country": "United Kingdom"
      },
      "date_of_creation": "2018-04-13",
      "kind": "searchresults#company",
      "snippet": "",
      "address_snippet": "27  Old Gloucester Street, London, United Kingdom, WC1N 3AX"
    },
  ],
  "kind": "search#companies",
  "start_index": 0,
  "page_number": 1,
  "total_results": 184
}

登录控制台

console

1 个答案:

答案 0 :(得分:2)

问题:

您看到的行为是因为您要遍历返回数据的所有属性,然后尝试遍历每个属性的'items'属性。这将导致您成功返回一次,然后返回16个(或json具有许多属性)未定义的返回。

解决方案:

您可以重构此块,而不必使用嵌套的$ .each循环:

success: function(data){
          $.each(data, function(){
              $.each(this, function(key, val) {
                  console.log(val.title + " : company number : " + val.company_number);
                  $('[data-select]').append(`
                      <option value='`+key+`'>`+val.title+`</option>
                  `);
              });
          });
      },

...到一个简单得多的版本:

success: function(data){
            $.each(data.items, function(key, val) {
                console.log(val.title + " : company number : " + val.company_number);
                $('[data-select]').append(`
                    <option value='`+key+`'>`+val.title+`</option>
                `);
              });
      },

在上面的代码块中,我摆脱了外部的$ .each循环,而是将内部循环更改为以“ data.items”为目标。

希望有帮助。