ã¯ãªã¼ã³ãª JSONP ãªã¯ã¨ã¹ã
åºæ¬æ¦å¿µ
JSONP (JSON with Padding) ã¨ã¯ãHTML ææ¸ã«ããã¦ãJavascript ã使ã£ã¦ SCRIPT è¦ç´ ãåçã«è¿½å ãããã¨ã«ããããªã¢ã¼ããµã¼ãã¸ã®ãªã¯ã¨ã¹ãã»ãã¼ã¿ã®åå¾ãå¯è½ã«ããæè¡ã§ããã
JSON ã¨ããååã¯ã¤ãã¦ããããåå¾ã§ãããã¼ã¿ã¯ JSON å½¢å¼ã«éããªãã(XMLHttpRequest ã®åå¾ãã¼ã¿ã XML å½¢å¼ã¨éããªãã®ã¨ãç®èã«ãä¼¼ã¦ãã) SCRIPT è¦ç´ ã追å ããã¨ããã©ã¦ã¶ã¯ Javascript ãã¡ã¤ã«ããªã¢ã¼ããµã¼ãããèªã¿è¾¼ããã¨ãããããã® Javascript ãã¡ã¤ã«ã¯ãèªã¿è¾¼ã¾ããã¨åæã«å®è¡ãããã®ã§ãããã§èªã¿è¾¼ã¿å
ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã¨ãªãé¢æ°ãå¼ã³åºãããµã¼ãããåå¾ãããã¼ã¿ãåã渡ãã®ã§ããã
ãã£ã¨ãåç´åãããä¾ã示ãã
<!-- ãã©ã¦ã¶å´(jsonp_client.html) --> <html> <head> <script type="text/javascript"> // ã°ãã¼ãã«å¤æ° var JSONPHandler; function requestJSONP(url, oncomplete) { var sc = document.createElement("script"); sc.setAttribute("type","text/javascript"); sc.setAttribute("charset","UTF-8"); sc.setAttribute("src", url ); JSONPHandler = oncomplete; // HEAD è¦ç´ ã« SCRIPT è¦ç´ ã追å ããã document.getElementsByTagName("head")[0].appendChild(sc); } </script> <body> <!-- url ã®æ«å°¾ã® ? ã®å¾ãã«ãã©ã¡ã¼ã¿ãè¨è¿°ãã --> <a href="javascript:void" onclick='requestJSONP("http://example.com:3000/site/jsonp_server?name=Eiji", function(response) { alert(response); });'>Click here!</a> </body> </html>
# ãµã¼ãå´ (RAILS_ROOT/app/views/jsonp_server.rjs / Rails 1.2.5) # (Rails 2.0 ã§ã¯ CSRF 対çã®ããããã¾ãåããªãããã»ã»ã») # params[:name] ã«ã¯ URL 㧠?name=xxx ã¨æå®ãã xxx ãå ¥ã£ã¦ããã script =<<-EOS (function() { JSONPHandler("Hello, #{params[:name]}!"); })(); EOS page << script
æ¬è³ªçãªãã¨ã¯ãã¯ã©ã¤ã¢ã³ãå´ã¯ããµã¼ããè¿ã Javascript ã®ã³ã¼ããå®è¡å¯è½ãªãã¼ã¿åãåãç¨ã®é¢æ°ãç¨æããªããã°ãªããªããã¨ãããã¨ã§ãããï¼ä¸ã®ä¾ã§ã¯ JSONPHandler) ããã¯ããµã¼ããè¿ã Javascript ã³ã¼ããã¢ã¯ã»ã¹ã§ããããã«ãã°ãã¼ãã«ãªåå空éã«ç»é²ããã¦ããªããã°ãªããªãã
ããä¸è¬çãªã¤ã³ã¿ã¼ãã§ã¤ã¹
ä¸ã®ã³ã¼ãã¯åããã次ã®ç¹ãæ¹åå¯è½ã ã
- ãµã¼ãå´ã§ã¯ JSONP ãªã¯ã¨ã¹ãã®ãã³ã«ã¬ã¹ãã³ã¹ãã©ãããã Javascript ã³ã¼ããçæããªããã°ãªããªããã¨ã
- ã³ã¼ã«ããã¯é¢æ°ããªã¯ã¨ã¹ãå®äºå¾ãæ®ã£ã¦ãã¾ããã¨ã
- 追å ãã SCRIPT è¦ç´ ãæ®ã£ã¦ãã¾ããã¨ã
- ã®å¯¾çã¨ãã¦ã¯ãããããã«ã¼ãã³ã¯ã¼ã¯ãããªããã«ãã¡ã½ãããä½ã£ã¦ããã
- ã®å¯¾çã¨ãã¦ã¯ãå¿ ãåå¨ãããã¨ãä¿è¨¼ããã¦ãã window ãªãã¸ã§ã¯ãã«ã³ã¼ã«ããã¯é¢æ°ãç»é²ããå®è¡å¾ãã ã¡ã«åé¤ããã°ããã
- ã®å¯¾çã¨ãã¦ã¯ããã¯ãã³ã¼ã«ããã¯é¢æ°ã®å®è¡ç´å¾ã«SCRIPT è¦ç´ ãæ¶å»ããããã«ããã(èªåèªèº«ãç ´å£ããã¿ããã§æ°æã¡ãããããã¨ãããããã¾ãè¡ãããã )
ãã®å¯¾çãæ½ãããã®ã以ä¸ã®ã³ã¼ãã§ããã
<!-- ãã©ã¦ã¶å´(jsonp_client.html) --> <html> <head> <script type="text/javascript"> // ã°ãã¼ãã«å¤æ°ã使ç¨ãã¦ããªããã¨ã«æ³¨æ function requestJSONP(url, oncomplete) { var sc = document.createElement("script"); sc.setAttribute("type","text/javascript"); sc.setAttribute("charset","UTF-8"); sc.setAttribute("src", url ); // ååã®è¡çªãé¿ãã¦ã©ã³ãã ãªååãæ¡ç¨ããã sc.id = "__jsonpscript_xd6EY9c23w"; window["__jsonphandler__Absd23Xf87"] = oncomplete; // HEAD è¦ç´ ã« SCRIPT è¦ç´ ã追å ããã document.getElementsByTagName("head")[0].appendChild(sc); } </script> <body> <!-- url ã®æ«å°¾ã® ? ã®å¾ãã«ãã©ã¡ã¼ã¿ãè¨è¿°ãã --> <a href="javascript:void" onclick='requestJSONP("http://example.com:3000/site/jsonp_server?name=Eiji", function(response) { alert(response); });'>Click here!</a> </body> </html>
# ãµã¼ãå´ # RAILS_ROOT/app/helpers/application_helpr.rb module ApplicationHelper def respond_jsonp(response) render :update do |page| # å®è¡å¾ãã ã¡ã«ã³ã¼ã«ããã¯é¢æ°ã¨ SCRIPT è¦ç´ ãæ¶å» script =<<-EOS (function() { window["__jsonphandler__Absd23Xf87"](#{response.to_json}); delete window["__jsonphandler__Absd23Xf87"]; var sc = document.getElementById("__jsonpscript_xd6EY9c23w"); if(sc) sc.parentNode.removeChild(sc); })(); EOS page << script end end end # RAILS_ROOT/app/controllers/site_controller.rb def jsonp_server respond_jsonp("Bonjour, #{params[:name]}!") end
追è¨
2008/02/23 ä¸é¨ãµã³ãã«ã³ã¼ãä¿®æ£ã