在 getson 中获取 JSON
getJSON in getson
我有两个 JSON-数据看起来像这样:
[{
"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>");
}
});
}
它向我显示 Table 在正确的列和行中包含正确的产品,但尝试加入制造商名称的列显示“[object Object]”而不是制造商名称。
我找不到错误,谁能帮帮我..?
我猜你的 "Manufacturers-Name" 列是 td
和 manus
class。如果是这样,那么我认为问题是为什么 w.name
显示不正确。
$.getJSON
是异步的。它 returns 是一个承诺,而不是实际的 API 响应。所以 w
在这种情况下是一个承诺,而不是制造商数据。
您需要按照第一次使用的方式使用 $.getJSON
(通过传入 success
回调)。
我有两个 JSON-数据看起来像这样:
[{
"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>");
}
});
}
它向我显示 Table 在正确的列和行中包含正确的产品,但尝试加入制造商名称的列显示“[object Object]”而不是制造商名称。
我找不到错误,谁能帮帮我..?
我猜你的 "Manufacturers-Name" 列是 td
和 manus
class。如果是这样,那么我认为问题是为什么 w.name
显示不正确。
$.getJSON
是异步的。它 returns 是一个承诺,而不是实际的 API 响应。所以 w
在这种情况下是一个承诺,而不是制造商数据。
您需要按照第一次使用的方式使用 $.getJSON
(通过传入 success
回调)。