为什么我不能在表格中看到我的JSON数据?

时间:2017-05-30 02:19:07

标签: javascript jquery html json

我写这个问题是因为我有这个网站需要制作,我唯一的问题是JSON。 我有这个带有JSON的网址,但是当我尝试使用链接时它会给我一个错误,你可以在我的问题中看到更深层次,但是当从网址获取所有数据时我把它放在VAR然后我将得到没有错误,我会看到我的数据,但这不是应该发生的事情。

我的JSON遇到了这个问题,有些我怎么看不到我的HTML页面上的数据,我也找不到问题。

这里有我的HTML页面:

        <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>

在这里你有我的2个脚本我尝试了第一个我尝试从网址获取JSON第二个我在我的电脑上本地下载了JSON

<!-- <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>

如何设置数据: 所有JSON数据都来自此网址: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"}, 

这是我的控制台中的错误消息:XMLHttpRequest cannot load

现在我的问题是我如何解决问题,如果有一个或我错过了什么,为了让它工作,我也真的想通过URL获取数据,如第一个脚本所示

(我也只在这个项目中使用jQuery,Bootstrap和HTML。)

修改 问题已解决,因为我在我的电脑上本地做这个项目我必须启动一个本地服务器,我忘了这一切。 因此,如果其他任何人遇到一些问题请务必双重检查您有一个为项目运行的本地服务器

2 个答案:

答案 0 :(得分:0)

你的脚本似乎很好。通过消除过程,$.getJSON("unisport.json", function(data) { ... } );调用一定存在问题。您必须要么没有从服务器获得JSON响应,要么以预期的格式获得JSON。

尝试console.log收到的JSON响应,确认它是预期的格式,或者检查调试器,看看$.getJSON是否抛出了错误。

答案 1 :(得分:0)

选中此plunker

&#13;
&#13;
 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);
&#13;
<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>
&#13;
&#13;
&#13;