2013-11-27 54 views
0

我想解析這個json http://openclipart.org/search/json/?&query=christmas&page=1&amount=4在我的html頁面。解析OpenClipArt api json到Html

我的代碼是

<!doctype html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title>jQuery.getJSON demo</title> 
     <style> 
      img { 
       height: 200px; 
       float: left; 
      } 
     </style> 
     <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    </head> 
    <body> 
     <div id="images"></div> 
     <script> 
      (function() { 
       var api = "http://openclipart.org/search/json/?"; 
       $.getJSON(api, { 
        query: "christmas", 
        page: "1", 
        amount: "4" 
       }).done(function(data) { 
        $.each(data.payload, function(i, item) { 
         $("<img>").attr("src", item.svg.png_thumb).appendTo("#images"); 
         if (i === 3) { 
          return false; 
         } 
        }); 
       }); 
      })(); 
     </script> 
    </body> 
</html> 

http://jsfiddle.net/2n8ax/,但什麼是錯的。

+0

否 '訪問控制允許來源' 標頭出現在所請求的資源。 – CharliePrynn

+0

他們已經修復了它:DDD謝謝openclipart <3 – user3041529

回答

0

它不工作在jsFiddle的原因是由於同源策略。

這是什麼意思,你是而不是允許從jsfiddle.net訪問另一個域(http://openclipart.org/)上的資源。

看到這兩個資源:

  1. Same-origin policy
  2. Cross-origin resource sharing
+0

它不在我的電腦(本地)既不工作 – user3041529

+0

如果你打開你的控制檯,你會得到什麼錯誤? – CharliePrynn

+0

沒有,只有警告。有什麼辦法可以使它有效嗎? – user3041529