2

i write this question because i have this site i need to make, and my only problem is the JSON. i have this url with the JSON but when ever i try to work with the link it gives me an error, that you can see further down in my question, but when take all the data from the url i put it in a VAR then i will get no error and i will see me data, but that is not what is supposed to happen.

i got this problem with my JSON, some how i cant see my data on my HTML page, and i cant find the problem.

here you have my HTML page:

        <script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
        <script src="js/jquery-3,2,1.js" type="text/javascript"></script>
        <link href="css/bootstrap.min.css" rel="stylesheet">

    </head>

    <body>

        <div class="container-fluid">
            <table id="product_table" class="table table-responsive table-sm">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Photo</th>
                        <th>Sizes</th>
                        <th>Price</th>
                        <th>Old Price</th>
                        <th>Delivery</th>
                    </tr>
                </thead>

                <tbody>

                </tbody>
            </table>

        </div>
            <script src="js/bootstrap.js"></script>
            <script src="js/npm.js"></script>
        </div>
    </body>

</html>

and here you have my 2 scripts i tried out the first one i try to get the JSON from an url the second one i downloaded the JSON locally down on my pc

<!-- <script>
        var productJSON = "https://www.unisport.dk/api/sample/";
            $.getJSON(productJSON, function(data) {
                $.each(data.products, function(i,f){
                    var tblRow ="<tr>" + "<td>" + f.name + "</td>" + "<td>" + "<img class='img-responsive' alt='photo of product' src='" + f.image + "'>" + "</td>" + "<td>" + f.sizes + "</td>" + "<td>" + f.price + " "  + f.currency + "</td>" + "<td>" + "<s>" + f.price_old + " " + f.currency + "</s>" + "</td>" + "<td>" + f.delivery + "</td>" + "</tr>"
                     $(tblRow).appendTo("#products tbody");
            });
        });

    </script> -->

<script>
    $(document).ready(function(){
        $.getJSON("unisport.json", function(data){
            var product_data = '';
            $.each(data, function(key, value){
                product_data += '<tr>';
                product_data += '<td>'+value.name+'</td>';
                product_data += '<td><img scr="'+value.image+'"></td>';
                product_data += '<td>'+value.sizes+'</td>';
                product_data += '<td>'+value.price+' '+value.currency+'</td>';
                product_data += '<td>'+value.price_old+' '+value.currency+'</td>';
                product_data += '</tr>';
            });
            $('#product_table').append(product_data);
        });
    });    

</script>

how the data has been setup: all the JSON data is from this url: https://www.unisport.dk/api/sample/

{"is_customizable": "0", 
"delivery": "1-2 dage", 
"kids": "0", 
"name": "adidas Tr\u00e6ningsbukser Z.N.E. Road Trip - Gr\u00e5", 
"sizes": "XX-Large", 
"kid_adult": "0", 
"free_porto": "0", 
"image": "https://d2ij1pxeion66i.cloudfront.net/product/157128/010c01d20cac.jpg", 
"package": "0", 
"price": "399,00", 
"url": "https://www.unisport.dk/fodboldudstyr/adidas-trningsbukser-zne-road-trip-gra/157128/", 
"online": "1", 
"price_old": "799,00", 
"currency": "DKK", 
"img_url": "https://s3-eu-west-1.amazonaws.com/product-img/157128_maxi_0.jpg", 
"id": "157128", 
"women": "0"}, 

This is my error message in my console:XMLHttpRequest cannot load

Now my question is how does i fix the problem if there is one or what does i miss, in order to get it working, also i would really like to get the Data trough the URL as shown in the first script

(also i only use jQuery, Bootstrap and HTML in this project.)

EDIT Problem fixed, since i'm doing this project locally on my PC i had to start up a local server, that i forgot all about. So if any one else have some problems as me BE SURE TO DOUBLE CHECK that you have a local server running for the project

YoJuns
  • 23
  • 3
  • 2
    Please include JS code used. – EyuelDK May 30 '17 at 02:21
  • `and here you have my 2 scripts i tried out` - look again, there's nothing "here" - before you do anything, have you checked the browsers **developer** tools console for errors? (F12 to bring it up) – Jaromanda X May 30 '17 at 02:34
  • @JaromandaX sry about that somehow it posted it before i was done making the post, but i forgot all about looking for errors in my console, but i can see i get an "XMLHttpRequest cannot load" error – YoJuns May 30 '17 at 02:46
  • Thus there is a problem with your `$.getJSON("unisport.json", ...);` call. You are not getting a JSON response. The rest of your code looks fine, make sure the json path is correct. – EyuelDK May 30 '17 at 02:50

2 Answers2

0

Your script seems fine. By process of elimination, there must be a problem with the $.getJSON("unisport.json", function(data) { ... } ); call. You must either not be getting a JSON response from the server or the JSON in the expected format.

Either attempt to console.log the received JSON response to affirm it is in the expected format or check your debugger to see if $.getJSON has thrown an error.

EyuelDK
  • 3,029
  • 2
  • 19
  • 27
  • Hello i get no error when i try to debug my site, and when i look in my console i get the XMLHttpRequest cannot load error, its like it can "find" the data/json but i dont have access to it? – YoJuns May 30 '17 at 06:24
  • The problem is fixed, i forgot to start my local server :D – YoJuns May 30 '17 at 06:56
0

Check this plunker

 var unisport = [{
"is_customizable": "0", 
"delivery": "1-2 dage", 
"kids": "0", 
"name": "adidas Tr\u00e6ningsbukser Z.N.E. Road Trip - Gr\u00e5", 
"sizes": "XX-Large", 
"kid_adult": "0", 
"free_porto": "0", 
"image": "https://d2ij1pxeion66i.cloudfront.net/product/157128/010c01d20cac.jpg", 
"package": "0", 
"price": "399,00", 
"url": "https://www.unisport.dk/fodboldudstyr/adidas-trningsbukser-zne-road-trip-gra/157128/", 
"online": "1", 
"price_old": "799,00", 
"currency": "DKK", 
"img_url": "https://s3-eu-west-1.amazonaws.com/product-img/157128_maxi_0.jpg", 
"id": "157128", 
"women": "0"
},
{
"is_customizable": "0", 
"delivery": "1-2 dage", 
"kids": "0", 
"name": "adidas Tr\u00e6ningsbukser Z.N.E. Road Trip - Gr\u00e5", 
"sizes": "XX-Large", 
"kid_adult": "0", 
"free_porto": "0", 
"image": "https://d2ij1pxeion66i.cloudfront.net/product/157128/010c01d20cac.jpg", 
"package": "0", 
"price": "399,00", 
"url": "https://www.unisport.dk/fodboldudstyr/adidas-trningsbukser-zne-road-trip-gra/157128/", 
"online": "1", 
"price_old": "799,00", 
"currency": "DKK", 
"img_url": "https://s3-eu-west-1.amazonaws.com/product-img/157128_maxi_0.jpg", 
"id": "157128", 
"women": "0"
},
{
"is_customizable": "0", 
"delivery": "1-2 dage", 
"kids": "0", 
"name": "adidas Tr\u00e6ningsbukser Z.N.E. Road Trip - Gr\u00e5", 
"sizes": "XX-Large", 
"kid_adult": "0", 
"free_porto": "0", 
"image": "https://d2ij1pxeion66i.cloudfront.net/product/157128/010c01d20cac.jpg", 
"package": "0", 
"price": "399,00", 
"url": "https://www.unisport.dk/fodboldudstyr/adidas-trningsbukser-zne-road-trip-gra/157128/", 
"online": "1", 
"price_old": "799,00", 
"currency": "DKK", 
"img_url": "https://s3-eu-west-1.amazonaws.com/product-img/157128_maxi_0.jpg", 
"id": "157128", 
"women": "0"
}]
 
 

var product_data = '';
$.each(unisport, function(key, value){
                product_data += '<tr>';
                product_data += '<td>'+value.name+'</td>';
                product_data += '<td><img style="height:50px;width:50px;" src="'+value.image+'"></td>';
                product_data += '<td>'+value.sizes+'</td>';
                product_data += '<td>'+value.price+' '+value.currency+'</td>';
                product_data += '<td>'+value.price_old+' '+value.currency+'</td>';
                product_data += '<td>'+value.delivery+'</td>';
                product_data += '</tr>';
            });
            $('#product_table').append(product_data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <div class="container-fluid">
            <table id="product_table" class="table table-responsive table-sm">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Photo</th>
                        <th>Sizes</th>
                        <th>Price</th>
                        <th>Old Price</th>
                        <th>Delivery</th>
                    </tr>
                </thead>

                <tbody>

                </tbody>
            </table>

        </div>
RonyLoud
  • 2,408
  • 2
  • 20
  • 25
  • Hello. I have tried to use plunker and done it the same way as you, tried both with the VAR to store my data, and from my json file, i also tried to get the data through an URL but, when i load op the side on my PC it wont load the data (i get anXMLHttpRequest cant load error) but when i do i on plunker i get no error and my data get putted in the table as i want, but how can it work on plunker but not on my pc? – YoJuns May 30 '17 at 06:19
  • May it help [here](https://stackoverflow.com/questions/3828982/xmlhttprequest-cannot-load-an-url-with-jquery) – RonyLoud May 30 '17 at 06:47
  • Hey, thank you for the help, i found out that i forgot to start a local server. also thanks for the site, i did not know about that. – YoJuns May 30 '17 at 06:56