{"id":359,"date":"2013-04-28T02:24:22","date_gmt":"2013-04-27T17:24:22","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2013\/04\/28\/jquerygaejson\/"},"modified":"2013-04-28T02:24:22","modified_gmt":"2013-04-27T17:24:22","slug":"jquerygaejson","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2013\/04\/28\/jquerygaejson\/","title":{"rendered":"jQuery\u3092\u3064\u304b\u3063\u3066GAE\u3068JSON\u3092\u3084\u308a\u3068\u308a"},"content":{"rendered":"<p> GW\u306b\u7a81\u5165\u3057\u305f\u306e\u3067\u3001\u306a\u3093\u3068\u304b\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u6642\u9593\u3092\u637b\u51fa\u3057\u3066\u3044\u304f\uff01\u305e\u3002<\/p>\n<p>jQuery \u306e Ajax \u3092\u5229\u7528\u3057\u3066\u3001GAE\u306bJSON\u30c7\u30fc\u30bf\u3092\u9001\u308a\u3001GAE\u5074\u3067\u51e6\u7406\u3057\u3066JSON\u3092\u9001\u308a\u8fd4\u3059\u691c\u8a3c\u3002<\/p>\n<h2>\u9001\u4fe1\u753b\u9762\u5074 jQuery \u5074\u3067\u306e\u5bfe\u5fdc<\/h2>\n<p>\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3057\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u3001POST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308b\u3002\u4ee5\u4e0b\u306f\u3001\u30dc\u30bf\u30f3\u62bc\u4e0b\u6642\u306e\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\u629c\u7c8b\u3002<\/p>\n<ol>\n<li>dataType \u3092 \u2018json\u2019\u3068\u3059\u308b <\/li>\n<li>contentType \u3092 &#8216;application\/json\u2019 \u3068\u3059\u308b (\u901a\u5e38\u306ePOST\u3060\u3068\u3001application\/x-www-form-urlencoded \u3067JSON\u6587\u5b57\u5217\u3068\u3057\u3066\u6e21\u3089\u306a\u3044) <\/li>\n<li>data \u3092 JSON.stringify \u3067\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308b <\/li>\n<\/ol>\n<pre class=\"prettyprint linenums\">$(&quot;#create_project&quot;).click(function(){\r\n    json_data = {name:'sample',data:'sample_data'};\r\n    $.ajax({\r\n        url: &quot;\/project&quot;,\r\n        type: &quot;POST&quot;,\r\n        dataType: &quot;json&quot;,\r\n        contentType: &quot;application\/json; charset=utf-8&quot;,\r\n        data: JSON.stringify(json_data),\r\n        success: function(data){\r\n            \r\n            \/\/ \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u8fd4\u3063\u3066\u304d\u305f\u304b\u3092\u78ba\u8a8d\r\n            alert(data.name);\r\n        }\r\n    });<\/pre>\n<h2>\u53d7\u4fe1\u5074GAE\u3067\u306e\u5bfe\u5fdc<\/h2>\n<p>Project \u30af\u30e9\u30b9\u306e\u4e2d\u3060\u3051\u304c\u8a72\u5f53\u306e\u51e6\u7406\u3002<\/p>\n<ol>\n<li>reuqest.body \u304b\u3089\u3001JSON \u6587\u5b57\u5217\u3092\u53d6\u5f97\u3059\u308b(request.POST \u3084 request.params \u306b\u306f\u683c\u7d0d\u3055\u308c\u306a\u3044) <\/li>\n<li>json.loads \u3067 JSON\u6587\u5b57\u5217\u304b\u3089\u3001\u8f9e\u66f8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5909\u63db(\u7279\u5b9a\u306e\u30af\u30e9\u30b9\u3078\u306e\u5909\u63db\u306f\u4eca\u5f8c\u78ba\u8a8d\u3059\u308b) <\/li>\n<li>GAE\u5074\u3067\u51e6\u7406\u3057\u305f\u8a3c\u3068\u3057\u3066\u3001\u5024\u3092\u5909\u66f4 <\/li>\n<li>\u30ec\u30b9\u30dd\u30f3\u30b9\u5074\u3082\u3001content-Type \u3092 application\/json \u3092\u6307\u5b9a\u3059\u308b <\/li>\n<li>\u8f9e\u66f8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u3001JSON\u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u3066\u5fdc\u7b54\u3059\u308b <\/li>\n<\/ol>\n<pre class=\"prettyprint linenums\"># -*- coding:utf-8 -*-\r\nimport webapp2\r\nfrom google.appengine.ext.webapp import template\r\nfrom google.appengine.api import users\r\nimport os\r\nimport json\r\n\r\nclass Project(webapp2.RequestHandler):\r\n    def post(self):\r\n        # \u30ea\u30af\u30a8\u30b9\u30c8\u304b\u3089JSON\u3092\u53d6\u5f97\u3059\u308b\r\n        json_data = self.request.body\r\n        # JSON \u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\r\n        obj = json.loads(json_data)\r\n        obj['name'] = obj['name'] + &quot; via server.&quot;\r\n        \r\n        # Content-Type \u306f application\/json\r\n        self.response.content_type = 'application\/json; charset=utf-8'\r\n        # \u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u3066\u8fd4\u3059\r\n        self.response.out.write(json.dumps(obj))\r\n\r\nclass MainPage(webapp2.RequestHandler):\r\n    \r\n    def get(self):\r\n        user = users.get_current_user()\r\n        if user:\r\n            login_url = users.create_logout_url(&quot;\/&quot;)\r\n            login_title = 'Logout'\r\n            user_nickname = user.nickname()\r\n        else:\r\n            login_url =users.create_login_url(&quot;\/&quot;)\r\n            login_title = 'Login'\r\n            user_nickname = ''\r\n            \r\n        template_values = {'login_url':login_url,\r\n                           'login_title':login_title,\r\n                           'user_nickname':user_nickname }\r\n        \r\n        \r\n        path = os.path.join(os.path.dirname(__file__),'templates\/index.html')\r\n        self.response.out.write(unicode(template.render(path, template_values)))\r\n\r\napplication = webapp2.WSGIApplication([\r\n    ('\/', MainPage),\r\n    ('\/project', Project),\r\n], debug=True)\r\n\r\ndef main():\r\n    application.run()\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n    main()<\/pre>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/jQueryGAEJSON_1D0D\/jquery_gae_json01.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"jquery_gae_json01\" style=\"border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px\" border=\"0\" alt=\"jquery_gae_json01\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/jQueryGAEJSON_1D0D\/jquery_gae_json01_thumb.jpg\" width=\"372\" height=\"134\" \/><\/a><\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u3068\u308a\u3042\u3048\u305a\u3001\u60f3\u5b9a\u901a\u308a\u306b\u306f\u52d5\u3044\u305f\u3088\u3002\u3068\u3002<\/p>\n<p><iframe style=\"height: 240px; width: 120px\" marginheight=\"0\" src=\"http:\/\/rcm-jp.amazon.co.jp\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea09-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=4798123021\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe><iframe style=\"height: 240px; width: 120px\" marginheight=\"0\" src=\"http:\/\/rcm-jp.amazon.co.jp\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea09-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=4797357606\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe><iframe style=\"height: 240px; width: 120px\" marginheight=\"0\" src=\"http:\/\/rcm-jp.amazon.co.jp\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea09-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=4873114683\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe><iframe style=\"height: 240px; width: 120px\" marginheight=\"0\" src=\"http:\/\/rcm-jp.amazon.co.jp\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea09-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=4798124281\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>GW\u306b\u7a81\u5165\u3057\u305f\u306e\u3067\u3001\u306a\u3093\u3068\u304b\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u6642\u9593\u3092\u637b\u51fa\u3057\u3066\u3044\u304f\uff01\u305e\u3002 jQuery \u306e Ajax \u3092\u5229\u7528\u3057\u3066\u3001GAE\u306bJSON\u30c7\u30fc\u30bf\u3092\u9001\u308a\u3001GAE\u5074\u3067\u51e6\u7406\u3057\u3066JSON\u3092\u9001\u308a\u8fd4\u3059\u691c\u8a3c\u3002 \u9001\u4fe1\u753b\u9762\u5074 jQuery \u5074\u3067\u306e\u5bfe\u5fdc \u30dc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[19,26],"tags":[],"class_list":["post-359","post","type-post","status-publish","format-standard","hentry","category-googleappengine","category-jquery"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=359"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}