getJSON in getson

时间:2015-11-18 02:13:16

标签: jquery json ajax getjson

我有两个类似的JSON-Datas:

[{
  "productId": "1",
  "name": "RX7048",
  "imageUrl": "http://s7d9.scene7.com/is/image",
  "description": "Modern design.",
  "price": "229.95",
  "manufacturerId": "1"
}, {
  "productId": "2",
  "name": "RB 2132 52 New Wayfarer",
  "imageUrl": "http://s7d9.scene7.com/is/image",
  "description": "Best Seller",
  "price": "129.95",
  "manufacturerId": "1"
}, {
  ...
}]

{
  "1": {
    "manufacturerId": "1",
    "name": "Ray-Ban",
    "city": "",
    "state": "",
    "website": ""
  },
  "2": {
    "manufacturerId": "2",
    ...
  }
}

我试图在HTML网站上显示它们:

function getAllProducts() {
  $.getJSON('http://meyecare.herokuapp.com/api/v1/products', function(data) {
    var len = data.length;
    for(var i = 0; i < len; i++) {
      //get the name of the Manufacturer by Manufact.-Id         
      var w = $.getJSON('http://meyecare.herokuapp.com/api/v1/manufacturers/' + data[i].manufacturerId);

      $('#products').append("<tr><td class='name'>" + data[i].name +
                            "</td><td><img class='prodImg' src='" + 
                            data[i].imageUrl +  "'/>"+
                            "<td class='manus'>" + w.name + "</td>"+
                            "<td class='descr'>" + data[i].description+
                            "</td>" +
                            "<td>$ " + data[i].price+
                            "</td><td><span class='btn' prodId='" +
                            data[i].productId + "'>Buy me</span></td></tr>");
    }        
  });
}

它在正确的列和行中显示带有正确产品的表,但是尝试加入制造商名称的列显示&#34; [对象对象]&#34;而不是Manufacturername。

我找不到错误,有人可以帮我吗??

1 个答案:

答案 0 :(得分:0)

我猜您的“制造商名称”列是带有td类的manus。如果是这种情况,那么我认为问题是为什么w.name无法正确显示。

$.getJSON是异步的。它返回一个promise而不是实际的API响应。因此,w在这种情况下是承诺,而不是制造商数据。

您需要首次使用$.getJSON方式(通过传入success回调)。