{"id":2171,"date":"2017-09-22T20:16:04","date_gmt":"2017-09-22T11:16:04","guid":{"rendered":"http:\/\/alaki.co.jp\/blog\/?p=2171"},"modified":"2017-09-22T20:16:04","modified_gmt":"2017-09-22T11:16:04","slug":"ajax%e5%87%a6%e7%90%86%e3%82%92%e5%ae%9f%e8%a3%85%e3%81%97%e3%82%88%e3%81%86%ef%bc%81%ef%bc%88%e3%82%b5%e3%83%bc%e3%83%90%e3%83%bc%e3%82%b5%e3%82%a4%e3%83%89%e7%b7%a8%ef%bc%89","status":"publish","type":"post","link":"http:\/\/test.alaki.co.jp\/blog\/?p=2171","title":{"rendered":"Ajax\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u3088\u3046\uff01\uff08\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u7de8\uff09"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001Web\u30d7\u30ed\u30b0\u30e9\u30de\u306e\u7be0\u7530\u3067\u3059\u3002<\/p>\n<p>\u5c11\u3057\u305a\u3064\u3001\u6dbc\u3057\u3044\u65e5\u304c\u5897\u3048\u3066\u304d\u3066\u3001\u305d\u308d\u305d\u308d\u30bf\u30aa\u30eb\u30b1\u30c3\u30c8\u4e00\u679a\u3067\u5bdd\u308b\u306e\u306f\u5371\u967a\u306a\u5b63\u7bc0\u306b\u306a\u3063\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u5929\u6c17\u304c\u826f\u3051\u308c\u3070\u3001\u9031\u672b\u306b\u3067\u3082\u51ac\u670d\u3092\u6d17\u6fef\u3057\u3066\u3001\u8863\u66ff\u3048\u306e\u6e96\u5099\u3092\u59cb\u3081\u3088\u3046\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001\u524d\u56de\u306e\u300c<a href=\"https:\/\/alaki.co.jp\/blog\/?p=2133\" target=\"_blank\">Ajax\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u3088\u3046\uff01\uff08\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u7de8\uff09<\/a>\u300d\u306b\u5f15\u304d\u7d9a\u304d\u300cAjax\u300d\u3092\u4f7f\u3063\u305f\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u5074\u306e\u4ed5\u7d44\u307f\u306b\u3064\u3044\u3066\u3001\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u30d5\u30ed\u30f3\u30c8\u304b\u3089\u9001\u3089\u308c\u3066\u304f\u308b\u30c7\u30fc\u30bf\u306e\u78ba\u8a8d<\/h2>\n<p>\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001PHP\u3067\u7528\u610f\u3059\u308b\u3068\u3044\u3046\u4eee\u5b9a\u3067\u9032\u3081\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u524d\u306b\u4f5c\u6210\u3057\u305fAjax\u51e6\u7406\u3092\u3082\u3046\u4e00\u5ea6\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >$.ajax({\r\n\tasync: true,\/\/ \u975e\u540c\u671f\u51e6\u7406\r\n\turl: 'http:\/\/localhost\/getName.php',\r\n\ttype: \"POST\",\r\n\tdataType: 'json',\r\n\tdata: {\r\n\t\t'user_id': 21\r\n\t}\r\n}).done(function (data) {\r\n\tconsole.log(data.name + \"\u3092\u53d6\u5f97\u3057\u307e\u3057\u305f\u3002\");\r\n}).fail(function () {\r\n\tconsole.log(\"\u51e6\u7406\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002\");\r\n});\r\n<\/pre>\n<p>\u9001\u308a\u5148\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u300chttp:\/\/localhost\/getName.php\u300d\u3067\u3059\u3002<\/p>\n<p>\u9001\u4fe1\u65b9\u6cd5\u306f\u300cPOST\u300d\u3001\u623b\u308a\u5024\u306f\u300cJSON\u300d\u3067\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u9001\u308b\u30c7\u30fc\u30bf\u306f\u300c\u6dfb\u5b57\uff1auser_id\u300d\u306b\u300c21\u300d\u304c\u4ee3\u5165\u3055\u308c\u3066\u3044\u308b\u72b6\u614b\u3067\u3059\u3002<\/p>\n<h2>PHP\u3067\u53d7\u3051\u53d6\u308b\u3068\u304d\u306e\u30dd\u30a4\u30f3\u30c8<\/h2>\n<p>\u53d7\u3051\u53d6\u308a\u5074\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u300cgetName.php\u300d\u3068\u3057\u3066\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u203b\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3084\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306f\u4e00\u5207\u8003\u616e\u3057\u307e\u305b\u3093\u306e\u3067\u3001\u3042\u3057\u304b\u3089\u305a\u3002<\/p>\n<p>Ajax\u306e\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u308b\u3068\u304d\u306e\u30dd\u30a4\u30f3\u30c8\u306f\uff12\u3064\u3067\u3059\u3002<\/p>\n<h3>\u9001\u4fe1\u65b9\u6cd5\u306b\u3088\u308b\u30c7\u30fc\u30bf\u306e\u53d7\u3051\u53d6\u308a<\/h3>\n<p>Ajax\u306b\u9650\u3063\u305f\u3053\u3068\u3067\u306f\u306a\u3044\u3067\u3059\u304c\u3001\u3069\u3046\u3044\u3063\u305f\u65b9\u6cd5\u3067\u30c7\u30fc\u30bf\u304c\u9001\u3089\u308c\u3066\u304f\u308b\u304b\u3068\u3044\u3046\u306e\u306f\u91cd\u8981\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u3067\u3042\u308c\u3070\u300cPOST\u300d\u3067\u9001\u3089\u308c\u3066\u304f\u308b\u306e\u3067\u3001\u53d7\u3051\u53d6\u308a\u5074\u306f\u300cPOST\u300d\u5f62\u5f0f\u3067\u30c7\u30fc\u30bf\u3092\u5f85\u3061\u69cb\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u3069\u3046\u3044\u3063\u305f\u540d\u524d\uff08\u6dfb\u5b57\uff09\u304c\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u304b\u3068\u3044\u3046\u306e\u3082\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<p>\u53d7\u3051\u53d6\u308a\u5074\u3067\u306f\u300c$_POST[&#8216;user_id&#8217;]\u300d\u3068\u3057\u3066\u3001\u300c21\u300d\u3068\u3044\u3046\u5024\u3092\u53d7\u3051\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>\u30d5\u30ed\u30f3\u30c8\u3078\u5024\u3092\u8fd4\u3059\u623b\u308a\u5024\u306e\u578b<\/h3>\n<p>\u6b21\u306b\u91cd\u8981\u306b\u306a\u308b\u306e\u304c\u3001\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u3067\u51e6\u7406\u3092\u3057\u305f\u5185\u5bb9\u3092\u3069\u306e\u3088\u3046\u306b\u3057\u3066\u3001\u30d5\u30ed\u30f3\u30c8\uff08\u30d6\u30e9\u30a6\u30b6\uff09\u306b\u8fd4\u305b\u3070\u826f\u3044\u306e\u304b\u3068\u3044\u3046\u70b9\u3067\u3059\u3002<\/p>\n<p>Ajax\u51e6\u7406\u306b\u3088\u3063\u3066\u306f\u3001\u30d5\u30ed\u30f3\u30c8\u304b\u3089\u6295\u3052\u3063\u3071\u306a\u3057\u3067\u3001\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u3067\u306e\u51e6\u7406\u7d50\u679c\u3092\u8fd4\u3055\u306a\u3044\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306e\u5834\u5408\u306f\u3001\u623b\u308a\u5024\u304c\u5fc5\u8981\u3068\u306a\u308a\u300cJSON\u300d\u3067\u623b\u3057\u3066\u6b32\u3057\u3044\u3068\u3044\u3046\u6307\u793a\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>PHP\u3067\u300cJSON\u300d\u3092\u53d6\u308a\u6271\u3046\u6642\u306b\u5229\u7528\u3059\u308b\u306e\u304c\u300cjson_encode\u300d\u95a2\u6570\u3067\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u30d5\u30ed\u30f3\u30c8\u5074\u3067\u300cdata.name\u300d\u3068\u3057\u3066\u3001\u5229\u7528\u3057\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u9023\u60f3\u914d\u5217\u3067\u4f5c\u6210\u3057\u305f\u300cJSON\u300d\u30c7\u30fc\u30bf\u3092\u623b\u308a\u5024\u3068\u3057\u3066\u7528\u610f\u3059\u308b\u3068\u826f\u3044\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<h2>\u300cgetName.php\u300d\u3092\u4f5c\u6210\u3059\u308b<\/h2>\n<p>\u5148\u7a0b\u306e\uff12\u70b9\u306e\u30dd\u30a4\u30f3\u30c8\u3092\u8e0f\u307e\u3048\u3066\u3001\u300cgetName.php\u300d\u3092\u4f5c\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:php decode:true \" >&lt;?php\r\n\/\/\u9001\u3089\u308c\u3066\u304f\u308buser_id\u3092POST\u3067\u53d7\u3051\u53d6\u308a\u307e\u3059\u3002\r\n$userId = $_POST['user_id'];\r\n\r\n\/\/\u3053\u306e\u90e8\u5206\u3092\u30c7\u30fc\u30bf\u30fc\u30d9\u30fc\u30b9\u304b\u3089\u306e\u53d6\u5f97\u51e6\u7406\u306b\u5909\u3048\u308b\u3053\u3068\u3067\u67d4\u8edf\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u306a\u308a\u307e\u3059\u3002\r\nif ($userId == 21) {\r\n\t$userList['name'] = \"\u7be0\u7530\";\r\n} else {\r\n\t\/\/\u30e6\u30fc\u30b6\u30fcID\u304c\u4e00\u81f4\u3057\u306a\u3051\u308c\u3070false\u3092\u8868\u793a\u3057\u3066\u30a8\u30e9\u30fc\u3068\u3057\u307e\u3059\u3002\r\n\techo false;\r\n}\r\n\r\n\/\/\u9023\u60f3\u914d\u5217\u300c$userList\u300d\u3092JSON\u306b\u5909\u63db\u3057\u3066\u8868\u793a\u3057\u307e\u3059\u3002\r\necho json_encode($userList);<\/pre>\n<p>\u30d5\u30ed\u30f3\u30c8\u3078\u306e\u623b\u3057\u65b9\u306f\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306a\u3069\u306b\u3088\u3063\u3066\u5909\u308f\u308a\u307e\u3059\u304c\u3001\u300cecho\u300d\u307e\u305f\u306f\u300creturn\u300d\u3067\u3001\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u6b63\u5e38\u306b\u52d5\u3051\u3070\u3001\u30d6\u30e9\u30a6\u30b6\u306e\u30b3\u30f3\u30bd\u30fc\u30eb\u753b\u9762\u306b\u300c\u7be0\u7530\u3092\u53d6\u5f97\u3057\u307e\u3057\u305f\u3002\u300d\u3068\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>Ajax\u306b\u3088\u308b\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u8003\u616e<\/h2>\n<p>Ajax\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u306b\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u9762\u3067\u306e\u8003\u616e\u304c\u5fc5\u8981\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\uff08\uff11\uff09\u76f4\u63a5\u3001\u547c\u3073\u51fa\u3057\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u5b9f\u884c\u3055\u308c\u3066\u3082\u8d77\u52d5\u3057\u306a\u3044\u3002<\/p>\n<p>\uff08\uff12\uff09CSRF\uff08\u30af\u30ed\u30b9\u30fb\u30b5\u30a4\u30c8\u30fb\u30ea\u30af\u30a8\u30b9\u30c8\u30fb\u30d5\u30a9\u30fc\u30b8\u30a7\u30ea\uff09\u5bfe\u7b56\u3092\u3059\u308b\u3002<\/p>\n<p>\uff08\uff13\uff09\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30c1\u30a7\u30c3\u30af\u306b\u3088\u308b\u3001\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u78ba\u8a8d\u3092\u3059\u308b\u3002<\/p>\n<p>SQL\u3092\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u3067\u5229\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u5f53\u7136\u300cSQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u300d\u306e\u5bfe\u7b56\u3082\u5fc5\u8981\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u666e\u6bb5\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u3068\u540c\u69d8\u304b\u305d\u308c\u4ee5\u4e0a\u306b\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u5bfe\u7b56\u306f\u614e\u91cd\u306b\u884c\u3046\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\uff12\u56de\u306b\u5206\u3051\u3066\u3001Ajax\u306e\u7c21\u5358\u306a\u5b9f\u88c5\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>Ajax\u51e6\u7406\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u30e6\u30fc\u30b6\u30fc\u306b\u3068\u3063\u3066\u3088\u308a\u30ea\u30c3\u30c1\uff08\u6b7b\u8a9e\u3067\u3059\u304b\u306d\uff57\uff09\u3067\u3001\u30b9\u30c8\u30ec\u30b9\u304c\u5c11\u306a\u3044Web\u30b5\u30fc\u30d3\u30b9\u3092\u5b9f\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u662f\u975e\u3001Ajax\u3092\u30d7\u30ed\u30b0\u30e9\u30e0\u958b\u767a\u306b\u6d3b\u7528\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001Web\u30d7\u30ed\u30b0\u30e9\u30de\u306e\u7be0\u7530\u3067\u3059\u3002 \u5c11\u3057\u305a\u3064\u3001\u6dbc\u3057\u3044\u65e5\u304c\u5897\u3048\u3066&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":2173,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2171"}],"collection":[{"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2171"}],"version-history":[{"count":5,"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2171\/revisions"}],"predecessor-version":[{"id":2177,"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2171\/revisions\/2177"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=\/wp\/v2\/media\/2173"}],"wp:attachment":[{"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2171"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/test.alaki.co.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}