2017-05-30 40 views
2

我写这个问题,因为我有我需要做的这个网站,我唯一的问题是JSON。 我有这个网址与JSON,但是当我试图与链接工作,它给了我一个错误,你可以看到我的问题进一步下来,但是当从网址中的所有数据我把它放在一个VAR然后我会得到任何错误,我会看到我的数据,但这不是应该发生的。为什么不能在我的表格中看到我的JSON数据?

我得到了这个问题与我的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,引导和HTML在这个项目。)

编辑 问题固定的,因为我在做这个项目本地在我的电脑上,我不得不启动本地服务器,我忘了al我约。 因此,如果任何其他人有一些问题,我一定要仔细检查你有一个本地服务器的项目

+2

请包括JS代码。 – EyuelDK

+0

'这里你有我的2个脚本我试过了 - 再看一遍,这里没有什么 - 在你做任何事之前,你有没有检查浏览器**开发人员**工具控制台的错误? (F12来提起) –

+0

@JaromandaX sry关于它以某种方式发布它之前,我已完成发布,但我忘了所有关于在我的控制台中查找错误,但我可以看到我得到一个“XMLHttpRequest无法加载”错误 – YoJuns

回答

0

检查使用该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>

+0

你好。 我试图使用plunker并以与你一样的方式完成它,试图用VAR来存储我的数据,并从我的json文件,我也尝试通过URL获取数据,但是,当我加载op侧在我的电脑上它不会加载数据(我得到一个XMLHttpRequest不能加载错误),但是当我做我蹲在地板上我得到没有错误,我的数据被提出在表中,因为我想,但它怎么可以工作在蹲而不是在我的PC? – YoJuns

+0

可以帮助[这里](https://stackoverflow.com/questions/3828982/xmlhttprequest-cannot-load-an-url-with-jquery) – RonyLoud

+0

嘿,谢谢你的帮助,我发现我忘了启动本地服务器。 也感谢您的网站,我不知道这一点。 – YoJuns

0

你的脚本似乎罚款运行。通过淘汰过程,$.getJSON("unisport.json", function(data) { ... });呼叫必定会有问题。您必须不是从服务器或JSON以预期的格式获得JSON响应。

要么尝试console.log收到的JSON响应,以确认它是在预期的格式或检查您的调试器,看看是否$.getJSON已引发错误。

+0

你好,当我尝试调试我的网站时,我得到没有错误,当我看我的控制台我得到的XMLHttpRequest无法加载错误,它喜欢它可以“找到”数据/ JSON,但我没有访问它? – YoJuns

+0

问题已修复,我忘了启动我的本地服务器:D – YoJuns

相关问题