{"id":9267,"date":"2012-03-19T21:00:54","date_gmt":"2012-03-19T12:00:54","guid":{"rendered":"http:\/\/www.sssg.org\/blogs\/hiro345\/?p=9267"},"modified":"2012-03-20T12:07:53","modified_gmt":"2012-03-20T03:07:53","slug":"dart%e3%81%a7jsonp","status":"publish","type":"post","link":"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/9267.html","title":{"rendered":"Dart\u3067JSONP"},"content":{"rendered":"<p><a href=\"http:\/\/blog.sethladd.com\/2012\/03\/jsonp-with-dart.html\">Seth Ladd&#8217;s Blog: JSONP with Dart<\/a>\u306bJSONP\u3092Dart\u3067\u4f7f\u3046\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8aac\u660e\u304c\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u3069\u3046\u3059\u308c\u3070\u826f\u3044\u304b\u3001\u78ba\u8a8d\u3092\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n<!--more--><\/p>\n<p>Dart\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002Dart Editor \u3067\u65b0\u898f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u4f5c\u6210\u306e\u30a6\u30a3\u30b6\u30fc\u30c9\u304b\u3089\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u751f\u6210\u3057\u3066\u3001\u4fee\u6b63\u3057\u3066\u3044\u3051\u3070\u3044\u3044\u3067\u3057\u3087\u3046\u3002JSON\u30c7\u30fc\u30bf\u306e\u5c55\u958b\u306b\u3042\u305f\u3063\u3066\u306f\u3001parse\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u3044\u304f\u3064\u304b\u8981\u7d20\u3078\u306e\u30a2\u30af\u30bb\u30b9\u65b9\u6cd5\u306b\u3064\u3044\u3066\u3082\u793a\u3057\u3066\u3042\u308a\u307e\u3059\u3002\u5b9f\u969b\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u306f\u3001\u53d6\u308a\u51fa\u3057\u305f\u3044JSON\u30c7\u30fc\u30bf\u306b\u5408\u308f\u305b\u308c\u3070\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: java; gutter: true\">\r\n#import(&#039;dart:html&#039;);\r\n#import(&#039;dart:json&#039;);\r\n\r\ndataReceived(MessageEvent e) {\r\n  var data = JSON.parse(&#039;{&quot;responseData&quot;: &quot;sample1&quot;}&#039;);\r\n  String s = &quot;sample1 ... &quot; + data[&#039;responseData&#039;];\r\n  data = JSON.parse(&#039;{&quot;responseData&quot;: {&quot;results&quot;:&quot;sample2&quot;}}&#039;);\r\n  s =  s + &quot;&lt;br \/&gt;sample2 ... &quot; + data[&#039;responseData&#039;][&#039;results&#039;];\r\n  data = JSON.parse(&#039;{&quot;responseData&quot;: {&quot;results&quot;: {&quot;moreResultsUrl&quot;:&quot;http:\/\/example.jp\/&quot;}}, &quot;responseStatus&quot;: 200}&#039;);\r\n  s =  s + &quot;&lt;br \/&gt;sample3 ... &quot; + data[&#039;responseData&#039;][&#039;results&#039;][&#039;moreResultsUrl&#039;];\r\n  data = JSON.parse(&#039;&#039;&#039;\r\n   {\r\n    &quot;responseData&quot;: {\r\n      &quot;results&quot;:[{&quot;content&quot;:&quot;c&quot;, &quot;url&quot;:&quot;http:\/\/&quot;}],\r\n      &quot;cursor&quot;:{&quot;moreResultsUrl&quot;:&quot;http:\/\/example.jp\/&quot;}\r\n    }, \r\n    &quot;responseStatus&quot;: 200\r\n   }\r\n&#039;&#039;&#039;);\r\n  s =  s + &quot;&lt;br \/&gt;sample4 responseStatus ... &quot; + data[&#039;responseStatus&#039;];\r\n  s =  s + &quot;&lt;br \/&gt;sample4 moreResultsUrl ... &quot; + data[&#039;responseData&#039;][&#039;cursor&#039;][&#039;moreResultsUrl&#039;];\r\n  data = JSON.parse(e.data);\r\n  s =  s + &quot;&lt;br \/&gt;e.data responseStatus ... &quot; + data[&#039;responseStatus&#039;];\r\n  s =  s + &quot;&lt;br \/&gt;e.data results[0] url ... &quot; + data[&#039;responseData&#039;][&#039;results&#039;][0][&#039;url&#039;];\r\n  document.query(&#039;#status&#039;).innerHTML = s;\r\n}\r\nvoid main() {\r\n  window.on.message.add(dataReceived);\r\n  \r\n  ScriptElement script = new Element.tag(&quot;script&quot;);\r\n  script.src = &quot;https:\/\/ajax.googleapis.com\/ajax\/services\/search\/news?v=1.0&amp;q=barack%20obama&amp;callback=callbackForJsonpApi&quot;;\r\n  document.body.elements.add(script);\r\n}\r\n<\/pre>\n<p>\u5b9f\u969b\u306bJSONP\u7d4c\u7531\u306e\u30c7\u30fc\u30bf\u3092\u89e3\u6790\u3057\u3066\u3044\u308b\u306e\u306f\u3001data = JSON.parse(e.data) \u306e\u3068\u3053\u308d\u3067\u3059\u3002dataReceived()\u95a2\u6570\u306f\u3069\u3046\u3084\u3063\u3066\u547c\u3076\u304b\u3068\u3044\u3046\u3068\u3001\u5c11\u3005\u30c8\u30ea\u30c3\u30ad\u30fc\u306a\u3053\u3068\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u76e3\u8996\u3059\u308b\u3088\u3046\u306b\u3001\u300cwindow.on.message.add(dataReceived)\u300d\u3068\u3057\u3066\u3001dataReceived()\u95a2\u6570\u3092\u767b\u9332\u3057\u3066\u3044\u307e\u3059\u3002\u52d5\u7684\u306bJSONP API\u3092\u547c\u3073\u51fa\u3057\u305f\u3044\u3053\u3068\u304c\u591a\u3044\u306f\u305a\u306a\u306e\u3067\u3001ScriptElement\u3092\u4f7f\u3063\u3066\u30bf\u30b0\u3092\u52d5\u7684\u306b\u751f\u6210\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306eAPI\u3092\u6307\u5b9a\u3059\u308bURI\u3067\u3001\u300ccallback=callbackForJsonpApi\u300d\u3068JavaScript\u306e\u95a2\u6570\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>callbackForJsonpApi\u3068\u3044\u3046JavaScript\u306e\u95a2\u6570\u306f\u3001HTML\u5074\u306b\u7528\u610f\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u3053\u306e\u95a2\u6570\u304cJSONP API\u304b\u3089\u547c\u3073\u51fa\u3055\u308c\u308b\u306e\u3067\u3001\u3053\u3053\u306b\u6765\u305f\u5024\u3092dataReceived\u95a2\u6570\u3078\u4f1d\u642c\u3055\u305b\u308b\u305f\u3081\u306b\u3001postMessage()\u3092\u4f7f\u3063\u3066\u3001window\u3078\u30e1\u30c3\u30bb\u30fc\u30b8\u3092post\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: xhtml; gutter: true\">\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\nfunction callbackForJsonpApi(s) {\r\n  window.postMessage( JSON.stringify(s), &#039;*&#039;);\r\n}\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>\u4eca\u56de\u7528\u610f\u3057\u305fHTML\u3067\u306f\u3001JSONP\u3067\u53d6\u308a\u51fa\u3057\u305f\u5024\u306b\u3064\u3044\u3066\u7d50\u679c\u8868\u793a\u3092\u3059\u308b\u3088\u3046\u306b\u3001Dart\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u3042\u308f\u305b\u3066\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"brush: xhtml; gutter: true\">\r\n&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;jsonp&lt;\/title&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;h1&gt;jsonp&lt;\/h1&gt;\r\n    &lt;h2 id=&quot;status&quot;&gt;dart is not running&lt;\/h2&gt;\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\nfunction callbackForJsonpApi(s) {\r\n  window.postMessage( JSON.stringify(s), &#039;*&#039;);\r\n}\r\n&lt;\/script&gt;\r\n    &lt;script type=&quot;application\/dart&quot; src=&quot;jsonp.dart&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;http:\/\/dart.googlecode.com\/svn\/branches\/bleeding_edge\/dart\/client\/dart.js&quot;&gt;&lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001\u6b21\u306e\u3088\u3046\u306a\u7d50\u679c\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"brush: bash; gutter: true\">\r\njsonp\r\n\r\nsample1 ... sample1\r\nsample2 ... sample2\r\nsample3 ... http:\/\/example.jp\/\r\nsample4 responseStatus ... 200\r\nsample4 moreResultsUrl ... http:\/\/example.jp\/\r\ne.data responseStatus ... 200\r\ne.data results[0] url ... http%3A%2F%2Fwww.afpbb.com%2Farticle%2Flife-culture%2Freligion%2F2866309%2F8663407%3Fctm_campaign%3Dtxt_topics\r\n<\/pre>\n<p>\u9762\u767d\u3044\u3067\u3059\u306d\u3047\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Seth Ladd&#8217;s Blog: JSONP with Dart\u306bJSONP\u3092Dart\u3067\u4f7f\u3046\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8aac\u660e\u304c\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u3069\u3046\u3059\u308c\u3070\u826f\u3044\u304b\u3001\u78ba\u8a8d\u3092\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[638,674,1209],"class_list":["post-9267","post","type-post","status-publish","format-standard","hentry","category-program","tag-dart","tag-jsonp","tag-linux"],"_links":{"self":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/9267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/comments?post=9267"}],"version-history":[{"count":4,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/9267\/revisions"}],"predecessor-version":[{"id":9269,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/9267\/revisions\/9269"}],"wp:attachment":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/media?parent=9267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/categories?post=9267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/tags?post=9267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}