Ich möchte zahlreiche Produkte in einen Abschnitt der Seite laden, die Daten aus einer JSON-Datei enthält. Ich habe es mit JavaScript zu arbeiten, aber ich würde lieber jQuery verwenden. In der Konsole sind keine Fehler vorhanden, die Daten werden jedoch nicht geladen. Ich benutze Bootstrap. Relativ neu zu Ajax/JSON.JSON-Daten werden nicht mit jQuery geladen
HTML
<div class="cat-group">
<div id="content"></div>
</div
CSS
.cat-group { overflow: hidden; }
jQuery
$(function(){
function loadProducts() {
$.getJSON('products.json')
.done(function(data){
products = data;
}).fail(function() {
$('#content').html('Sorry! We could not load our products at the moment. Try again later!');
});
}
loadProducts();
$(window).on('load', function(){
var newContent = ''; // Build up timetable by
for (var i = 0; i < products.length; i++) { // looping through events
newContent += '<div class="col-lg-3 col-md-4 col-xs-6">';
newContent += '<a class="thumbnail">';
newContent += '<h4>SKU: <span>' + products[i].product + '</span></h4>';
newContent += '<img class="img-responsive" src="' + products[i].img + '">';
newContent += '</a>';
newContent += '</div>';
}
$('#content').html(newContent);
});
})
JSON Datei
{
"products": [
{
"product": "product1",
"price": "10",
"sku": "1",
"img": "img/prod-1.jpg"
},
{
"product": "product2",
"price": "12",
"sku": "2",
"img": "img/prod-2.jpg"
},
{
"product": "product3",
"price": "13",
"sku": "3",
"img": "img/prod-3.jpg"
},
{
"product": "product3",
"price": "14",
"sku": "4",
"img": "img/prod-4.jpg"
}
]
}
Druck/Konsole Ihre Daten –
@NitinDhomse alle Objekte in der Konsole –
Dann, wie Sie Daten erscheinen log wird sagen können nicht geladen? –