Jquery getson中的getJSON
我有两个类似的JSON数据:Jquery getson中的getJSON,jquery,json,ajax,getjson,Jquery,Json,Ajax,Getjson,我有两个类似的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",
[{
"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"
}, {
...
}]
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>");
}
});
}
及
我尝试在HTML网站中显示它们,如下所示:
[{
"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"
}, {
...
}]
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>");
}
});
}
函数getAllProducts(){
$.getJSON('http://meyecare.herokuapp.com/api/v1/products,函数(数据){
var len=data.length;
对于(变量i=0;itd
和manus
类。如果是这样,那么我假设问题是为什么w.name
不能正确显示
是异步的。它返回一个承诺,而不是实际的API响应。因此,在这种情况下,w
是一种承诺,而不是制造商数据
您需要使用
$.getJSON
您第一次使用它的方式(通过传入success
callback)。出于某种原因,它现在显示我“未定义”而不是“对象对象对象”,我尝试按照您的建议执行以下操作:函数getAllProducts(){$.getJSON('productsJSON',函数(数据){var len=data.length;for(var i=0;i)结果是is没有显示任何内容,既没有显示任何制造商名称,也没有显示表$getJSON('manufaJSON/theId')
的响应是什么?我想端点只返回制造商列表,而不是单个制造商。下面是一个示例,演示了我上面的建议。