I'd like to use d3.js to display new articles on Tena bookmarks.

Asked 2 years ago, Updated 2 years ago, 164 views

3 Answers

JSONP is a JSON that can be read as an external script, which is different from the usual JSON format.Therefore, the API provider needs to take action, but neither Qiita nor Hatena bookmarks are compatible.

Note: The appropriate API for Hatena bookmarks can be retrieved by JSONP by adding the callback parameter (see mjy's answer.

The response from Qiita on the demo site mentioned above has the following headers:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Origin: http://bl.ocks.org
Access-Control-Expose-Headers:
Access-Control-Max-Age: 1728000

This is a group of headers defined in Cross-Origin Resource Sharing (CORS) that are used for access control.The XMLHttpRequest used for Ajax communication, including d3.js, applies the rule Same-Origin Policy and basically cannot communicate between different origin, but only if permitted by these header groups.http://bl.ocks.org is allowed.

On the other hand, the Tena Bookmark API does not print these headers, so the browser interprets them as "rejects all requests from different origin" and blocks communication.

Other references


2022-09-30 21:11

There is a way to bite a site that allows cross-domain communication such as JSONProxy or allow-any-origin.appspot.com.

var url="http://b.hatena.ne.jp/entry/json/http://www.hatena.ne.jp/";
varproxy_url="http://allow-any-origin.appspot.com/"+encodeURIComponent(url);

d3.json(proxy_url, function(error,json){
  if(error){
    return console.warn(error);
  }
  $("#data").text(JSON.stringify({
    title —json.title,
    url —json.url,
    count —json.count
  }));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">/script><script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js">/script>

<h1>Hatena Bookmarks</h1>
<pred="data"></pre>

These sites are basically unguaranteed, but they are useful if you want to create something that works.

If necessary, you can create your own API return page and replace it later.


2022-09-30 21:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.