{"id":2233,"date":"2023-12-17T13:17:44","date_gmt":"2023-12-17T04:17:44","guid":{"rendered":"https:\/\/www.dogrow.net\/hp\/?p=2233"},"modified":"2023-12-17T15:15:41","modified_gmt":"2023-12-17T06:15:41","slug":"blog145","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/hp\/blog145\/","title":{"rendered":"(145) JavaScript\u306e\u30af\u30ed\u30b9\u30aa\u30ea\u30b8\u30f3\u5236\u7d04"},"content":{"rendered":"<h1 class=\"my_h\">1. CORS\u306b\u95a2\u3059\u308b\u60c5\u5831\u53ce\u96c6<\/h1>\n<p>CORS(Cross-Origin Resource Sharing)\u306b\u3064\u3044\u3066\u306f\u3001\u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u3092\u53c2\u7167\u3055\u308c\u305f\u3044\u3002<br \/>\n<a href=\"https:\/\/aws.amazon.com\/jp\/what-is\/cross-origin-resource-sharing\/\" target=\"_blank\" rel=\"noopener\">https:\/\/aws.amazon.com\/jp\/what-is\/cross-origin-resource-sharing\/<\/a><\/p>\n<p>CORS\uff08\u30b3\u30fc\u30ba\uff09\u3092\u7c21\u5358\u306b\u8aac\u660e\u3059\u308b\u3068\uff1f<br \/>\n\u30fb<span class=\"my_fw_bold\">CORS\u60c5\u5831\u3092\u63d0\u4f9b\u3059\u308b\u306e\u306f\uff1f<\/span> \u2192 \u30b5\u30fc\u30d0\u30fc\u5074<br \/>\n\u30fb<span class=\"my_fw_bold\">CORS\u60c5\u5831\u3092\u53c2\u7167\u3059\u308b\u306e\u306f\uff1f<\/span> \u2192 \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\uff08\uff1dWeb\u30d6\u30e9\u30a6\u30b6\uff09<br \/>\n\u30fb<span class=\"my_fw_bold\">CORS\u60c5\u5831\u3092\u53c2\u7167\u3059\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u306f\uff1f<\/span> \u2192 JavaScript\u5185\u306e\u30b3\u30fc\u30c9\u3067 HTTP\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u767a\u884c\u3057\u3001HTTP\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d7\u4fe1\u3057\u305f\u6642\u306b\u305d\u306e\u30d8\u30c3\u30c0\u5185\u306e CORS\u60c5\u5831 <span class=\"my_fc_blueB\">Access-Control-Allow-Origin<\/span> \u3092\u898b\u308b\u3002<br \/>\n\u30fb<span class=\"my_fw_bold\">CORS\u60c5\u5831\u3092\u53c2\u7167\u3057\u3066\u4f55\u3092\u3059\u308b\uff1f<\/span> \u2192 JavaScript\u5185\u3067\u30ec\u30b9\u30dd\u30f3\u30b9\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u53ef\u5426\u3092\u5224\u65ad\u3059\u308b\u3002\u5426\u306a\u3089\u3070\u53d7\u4fe1\u30c7\u30fc\u30bf\u3092\u6368\u3066\u3066\u3001JavaScript\u4e0a\u3067\u53d7\u4fe1\u5931\u6557\u51e6\u7406\u30eb\u30fc\u30c8\u3092\u5b9f\u884c\u3059\u308b\u3002\u3064\u307e\u308a\u3001<span class=\"my_fc_crimsonB\">\u53d7\u4fe1\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u51fa\u6765\u3066\u3044\u3066\u3082\u3001\u6368\u3066\u3066\u3057\u307e\u3046\u3002<\/span><\/p>\n<p><span class=\"my_fc_deeppinkBBig\">\u2234 CORS\u306f\u3001Web\u30d6\u30e9\u30a6\u30b6\u5074\u3067\u53d7\u4fe1\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u53ef\u5426\u3092\u5224\u65ad\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3059\u308b\u3002<\/span><\/p>\n<h1 class=\"my_h\">2. \u81ea\u4f5c\u306e\u30b5\u30a4\u30c8\u3067 Web API\u3092\u516c\u958b\u3059\u308b\u5834\u5408\u306b\u5fc5\u8981\u306a\u3053\u3068<\/h1>\n<h3 class=\"my_h\">(1) \u81ea\u4f5c\u306e\u30b5\u30a4\u30c8\u3067 <span class=\"my_fc_deeppinkB\">Web API\u3092\u4e00\u822c\u516c\u958b<\/span>\u3059\u308b\u5834\u5408<\/h3>\n<p>Web API\u30ea\u30af\u30a8\u30b9\u30c8\u306b\u5bfe\u3059\u308b\u5fdc\u7b54\u3092\u9001\u4fe1\u6642\u306b\u3001HTTP\u306e <span class=\"my_fc_blueB\">Access-Control-Allow-Origin<\/span> \u30d8\u30c3\u30c0\u3067\u30ef\u30a4\u30eb\u30c9\u30ab\u30fc\u30c9 <span class=\"my_fc_blueB\">*<\/span> \u3092\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u30b5\u30fc\u30d0\u30fc\u5074\u306e Web API\u51e6\u7406\u30d7\u30ed\u30b0\u30e9\u30e0\u304c PHP\u8a00\u8a9e\u3067\u66f8\u304b\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304f\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ CORS\u3092\u8a31\u53ef\u3059\u308b\u30d8\u30c3\u30c0\u30fc\u3092\u8a2d\u5b9a\r\nheader(&quot;Access-Control-Allow-Origin: *&quot;);\r\n<\/pre>\n<h3 class=\"my_h\">(2) \u81ea\u4f5c\u306e\u30b5\u30a4\u30c8\u3067 <span class=\"my_fc_deeppinkB\">Web API\u3092\u81ea\u30b5\u30a4\u30c8\u306e\u30da\u30fc\u30b8\u3067\u306e\u307f\u4f7f\u7528\u53ef\u80fd\u306b\u5236\u9650<\/span>\u3059\u308b\u5834\u5408<\/h3>\n<p>\u81ea\u30b5\u30a4\u30c8\u304c https:\/\/www.example.com \u306e\u5834\u5408\u3001\u30b5\u30fc\u30d0\u30fc\u5074\u306e Web API\u51e6\u7406\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304f\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ CORS\u3092\u8a31\u53ef\u3059\u308b\u30d8\u30c3\u30c0\u30fc\u3092\u8a2d\u5b9a\r\nheader(&quot;Access-Control-Allow-Origin: https:\/\/www.example.com&quot;);\r\n<\/pre>\n<h1 class=\"my_h\">3. \u5b9f\u969b\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u3063\u3066\u78ba\u8a8d\u3057\u3066\u307f\u305f\u3002<\/h1>\n<h2 class=\"my_h\">1) \u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4ed5\u69d8<\/h2>\n<p>\u7b2c1\u9805\u3001\u7b2c2\u9805\u306e\u4e8c\u3064\u306e\u5024\u3092\u6e21\u3059\u3068\u3001\u8db3\u3057\u7b97\u306e\u7d50\u679c\u3092\u8fd4\u3057\u3066\u304f\u308c\u308b Web API\u3092\u4f5c\u3063\u3066\u307f\u305f\u3002<br \/>\n\u30df\u30cb\u30de\u30e0\u7cfb\u3092\u4f5c\u3063\u3066\u52d5\u4f5c\u78ba\u8a8d\u3059\u308b\u3068\u3001\u7269\u4e8b\u306e\u672c\u8cea\u304c\u969b\u7acb\u3063\u3066\u898b\u3048\u3066\u304f\u308b\u3002<\/p>\n<p>\u753b\u9762\u4ed5\u69d8\u306f\u305f\u3063\u305f\u306e\u3053\u308c\u3060\u3051\u3060\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/hp\/wp-content\/uploads\/2023\/12\/Image1.jpg\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<h2 class=\"my_h\">2) \u30d7\u30ed\u30b0\u30e9\u30e0\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n<h3 class=\"my_h\">(1) index.html<\/h3>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n  &lt;title&gt;Blog No.145&lt;\/title&gt;\r\n  &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; \/&gt;\r\n  &lt;script src=&quot;script.js&quot;&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n  &lt;input type=&quot;text&quot; name=&quot;t1&quot; \/&gt; \uff0b &lt;input type=&quot;text&quot; name=&quot;t2&quot; \/&gt; \uff1d &lt;input type=&quot;text&quot; name=&quot;s&quot; disabled&gt;\r\n  &lt;input type=&quot;button&quot; id=&quot;BTN&quot; value=&quot;\u8a08\u7b97\u5b9f\u884c&quot;&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h3 class=\"my_h\">(2) style.css<\/h3>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n*{\r\n  margin:  0;\r\n  padding: 0;\r\n  box-sizing: border-box;\r\n}\r\nbody{\r\n  padding: 0.5rem;\r\n}\r\n<\/pre>\n<h3 class=\"my_h\">(3) script.js<\/h3>\n<p>URL\u306e\u30d1\u30b9\u306f\u3001\u5b9f\u969b\u306b Web API\u3092\u8a2d\u7f6e\u3057\u305f\u30b5\u30fc\u30d0\u30fc\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3002<br \/>\n\u3053\u3053\u3067\u306f\u9069\u5f53\u306a URL\u3092\u8a18\u3057\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ndocument.addEventListener(&quot;DOMContentLoaded&quot;, function() {\r\n  const inp_t1 = document.querySelector('input&#x5B;name=&quot;t1&quot;]');\r\n  const inp_t2 = document.querySelector('input&#x5B;name=&quot;t2&quot;]');\r\n  const inp_s  = document.querySelector('input&#x5B;name=&quot;s&quot;]');\r\n  const btn    = document.getElementById('BTN');\r\n\r\n  btn.addEventListener('click', function() {\r\n    let url = `https:\/\/www.example.com\/sample\/webapi.php?t1=${inp_t1.value}&amp;t2=${inp_t2.value}`;\r\n    fetch(url)\r\n      .then(resp =&gt; resp.json())\r\n      .then(data =&gt; {\r\n        inp_s.value = data.s;\r\n      })\r\n      .catch(error =&gt; {\r\n        console.error('Error fetching data:', error);\r\n      });\r\n  });\r\n});\r\n<\/pre>\n<h3 class=\"my_h\">(4) webapi.php<\/h3>\n<p>\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u3060\u3051\u3001\u4e0a\u8a18\u306e(1),(2),(3)\u3068\u306f\u7570\u306a\u308b\u30c9\u30e1\u30a4\u30f3\u306eWeb\u4e0a\u306b\u8a2d\u7f6e\u3059\u308b\u3002<span class=\"my_fc_crimsonB\">\u203bCORS\u306b\u95a2\u3059\u308b\u52d5\u4f5c\u78ba\u8a8d\u306e\u305f\u3081<\/span><br \/>\n\u524d\u8ff0\u306e\u3088\u3046\u306b\u30012\u884c\u76ee\u306e\u6709\u7121\u304c\u4eca\u56de\u306e\u5b9f\u9a13\u306e\u91cd\u8981\u30dd\u30a4\u30f3\u30c8\u3060\u3002<\/p>\n<pre class=\"brush: php; highlight: [2]; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n  header('Access-Control-Allow-Origin: *');\r\n  $resp&#x5B;'s'] = $_GET&#x5B;'t1'] + $_GET&#x5B;'t2'];\r\n  echo json_encode($resp, JSON_UNESCAPED_UNICODE);\r\n?&gt;\r\n<\/pre>\n<h2 class=\"my_h\">3) \u52d5\u4f5c\u78ba\u8a8d\u7d50\u679c<\/h2>\n<h3 class=\"my_h\">(1) Web API\u304b\u3089\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u306b CORS\u60c5\u5831\u3092\u542b\u3081\u306a\u3044\u5834\u5408<\/h3>\n<p>\u3059\u306a\u308f\u3061\u3001\u4e0a\u8a18\u306e 2)-(4) webapi.php \u306e 2\u884c\u76ee\u3001header()\u3092\u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8\u3057\u3066\u5b9f\u884c\u3059\u308b\u3002<\/p>\n<pre class=\"brush: php; highlight: [2]; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/\/header('Access-Control-Allow-Origin: *');\r\n  $resp&#x5B;'s'] = $_GET&#x5B;'t1'] + $_GET&#x5B;'t2'];\r\n  echo json_encode($resp, JSON_UNESCAPED_UNICODE);\r\n?&gt;\r\n<\/pre>\n<p>\u3053\u306e\u5834\u5408\u3001Web\u30d6\u30e9\u30a6\u30b6\u306e\u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bCORS\u306b\u3088\u308b\u30a8\u30e9\u30fc\u304c\u8868\u793a\u3055\u308c\u305f\u3002<br \/>\n\u3064\u307e\u308a\u3001\u53d7\u4fe1\u30c7\u30fc\u30bf\u304c Web\u30d6\u30e9\u30a6\u30b6\u306b\u3088\u308a\u6368\u3066\u3089\u308c\u3001JavaScript\u30b3\u30fc\u30c9\u306f\u53d7\u4fe1\u30a8\u30e9\u30fc\u306e\u30eb\u30fc\u30c8\u306b\u5206\u5c90\u3059\u308b\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/hp\/wp-content\/uploads\/2023\/12\/Image4-1.jpg\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<h3 class=\"my_h\">(2) Web API\u304b\u3089\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u306b CORS\u60c5\u5831\u3092\u542b\u3081\u308b\u5834\u5408<\/h3>\n<p>\u3059\u306a\u308f\u3061\u3001\u4e0a\u8a18\u306e 2)-(4) webapi.php \u306e 2\u884c\u76ee\u3001header()\u3092\u6709\u52b9\u306b\u3057\u3066\u5b9f\u884c\u3059\u308b\u3002<\/p>\n<pre class=\"brush: php; highlight: [2]; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n  header('Access-Control-Allow-Origin: *');\r\n  $resp&#x5B;'s'] = $_GET&#x5B;'t1'] + $_GET&#x5B;'t2'];\r\n  echo json_encode($resp, JSON_UNESCAPED_UNICODE);\r\n?&gt;\r\n<\/pre>\n<p>\u3053\u306e\u5834\u5408\u3001Web\u30d6\u30e9\u30a6\u30b6\u306e\u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u30a8\u30e9\u30fc\u306f\u8868\u793a\u3055\u308c\u305a\u3001JavaScript\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u6700\u5f8c\u307e\u3067\u5b9f\u884c\u3055\u308c\u308b\u3002<br \/>\n\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3001Web\u30da\u30fc\u30b8\u4e0a\u306b\u8db3\u3057\u7b97\u306e\u7d50\u679c\u304c\u8868\u793a\u3055\u308c\u305f\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/hp\/wp-content\/uploads\/2023\/12\/Image6-1.jpg\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>1. CORS\u306b\u95a2\u3059\u308b\u60c5\u5831\u53ce\u96c6 CORS(Cross-Origin Resource Sharing)\u306b\u3064\u3044\u3066\u306f\u3001\u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u3092\u53c2\u7167\u3055\u308c\u305f\u3044\u3002 https:\/\/aws.amazon.com\/jp\/what-is\/cros\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/hp\/blog145\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2233","post","type-post","status-publish","format-standard","hentry","category-javascriptjquery"],"views":647,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/hp\/wp-json\/wp\/v2\/posts\/2233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/hp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/hp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/hp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/hp\/wp-json\/wp\/v2\/comments?post=2233"}],"version-history":[{"count":41,"href":"https:\/\/www.dogrow.net\/hp\/wp-json\/wp\/v2\/posts\/2233\/revisions"}],"predecessor-version":[{"id":2300,"href":"https:\/\/www.dogrow.net\/hp\/wp-json\/wp\/v2\/posts\/2233\/revisions\/2300"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/hp\/wp-json\/wp\/v2\/media?parent=2233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/hp\/wp-json\/wp\/v2\/categories?post=2233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/hp\/wp-json\/wp\/v2\/tags?post=2233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}