0

I want to load numerous products into a section of the page uaing data from a JSON file. I have got it to work using JavaScript, but I would prefer to use jQuery. There are no errors in the console, yet the data isn't loading. I am using Bootstrap. Relatively new to Ajax/JSON.

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 file

 {
 "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"
 }
 ]
 }
4
  • print / console log your data Commented Jul 14, 2016 at 11:34
  • @NitinDhomse all objects appear in the console Commented Jul 14, 2016 at 11:36
  • Then how you can say data is not loading? Commented Jul 14, 2016 at 11:38
  • @NitinDhomse wrong choice of words. I meant the content (imgs, text) isn't loading in the browser Commented Jul 14, 2016 at 11:41

3 Answers 3

1

I have made some changes in your code, Try this

$(function(){

            function loadProducts() {                    
                $.getJSON('products.json')              
                .done( function(data){                      
                    products = data.products;
                    renderProducts();
                }).fail( function() {                       
                    $('#content').html('Sorry! We could not load our products at the moment. Try again later!');
                });
            }

            loadProducts();                                      


        });


        function renderProducts() {

            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);
        }
Sign up to request clarification or add additional context in comments.

Comments

0

You are treating the returning data as if it were an array of products, but it is really an object containing an array of products. try products = data.products; instead of products = data;

Comments

0

Try this one. just replace products = data with products = data.d;

$(function(){

 function loadProducts() {                    
   $.getJSON('products.json')              
   .done( function(data){                      
     products = data.d;                            
  }).fail( function() {                       
   $('#content').html('Sorry! We could not load our products at the moment. Try again later!');
 });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.