{"id":1205,"date":"2025-04-11T10:00:25","date_gmt":"2025-04-11T01:00:25","guid":{"rendered":"https:\/\/route-zero.com\/recruit\/route\/1205\/"},"modified":"2025-05-07T13:54:44","modified_gmt":"2025-05-07T04:54:44","slug":"1205","status":"publish","type":"route","link":"https:\/\/route-zero.com\/recruit\/route\/1205\/","title":{"rendered":"JavaScript\u3067CORS\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u3068\u304d\u306e\u539f\u56e0\u3068\u5bfe\u51e6\u6cd5\u3092\u5fb9\u5e95\u89e3\u8aac"},"content":{"rendered":"<div>\n<h2>CORS\u30a8\u30e9\u30fc\u3063\u3066\u306a\u306b\uff1f\u306a\u305c\u3053\u306e\u8a18\u4e8b\u304c\u5fc5\u8981\u306a\u306e\u304b<\/h2>\n<p>JavaScript\u3067\u5916\u90e8\u306eAPI\u3068\u9023\u643a\u3057\u3088\u3046\u3068\u3057\u305f\u3068\u304d\u3001\u6b21\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u3092\u898b\u305f\u3053\u3068\u304c\u3042\u308a\u307e\u305b\u3093\u304b\uff1f<\/p>\n<pre><code>Access to fetch at <span>'https:\/\/api.example.com\/data'<\/span> <span>from<\/span> origin <span>'http:\/\/localhost:3000'<\/span> has been blocked <span>by<\/span> CORS policy<\/code><\/pre>\n<p>\u300cAPI\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3088\u3046\u3068\u3057\u305f\u3060\u3051\u306a\u306e\u306b\u3001\u306a\u3093\u3067\u30d6\u30ed\u30c3\u30af\u3055\u308c\u308b\u306e\uff1f\u300d<br \/>\u300cCORS\u3063\u3066\u3088\u304f\u805e\u304f\u3051\u3069\u3001\u3069\u3046\u3059\u308c\u3070\u76f4\u305b\u308b\u306e\uff1f\u300d<\/p>\n<p>\u305d\u3093\u306a\u7591\u554f\u3084\u30e2\u30e4\u30e2\u30e4\u306b\u5fdc\u3048\u308b\u305f\u3081\u306b\u3001\u3053\u306e\u8a18\u4e8b\u3067\u306f\u300cCORS\u30a8\u30e9\u30fc\u306e\u610f\u5473\u30fb\u539f\u56e0\u30fb\u78ba\u8a8d\u30dd\u30a4\u30f3\u30c8\u30fb\u89e3\u6c7a\u65b9\u6cd5\u300d\u307e\u3067\u3001<strong>\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u4ed8\u304d\u3067<\/strong>\u308f\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<hr>\n<h2>CORS\u306e\u57fa\u672c\u3068JavaScript\u3067\u306e\u30a8\u30e9\u30fc\u4f8b<\/h2>\n<h3>CORS\u3092\u3056\u3063\u304f\u308a\u89e3\u8aac<\/h3>\n<p>CORS\uff08Cross-Origin Resource Sharing\uff09\u3068\u306f\u3001\u300c\u7570\u306a\u308b\u5834\u6240\u306b\u3042\u308bWeb\u30b5\u30fc\u30d3\u30b9\u3068\u306e\u3084\u308a\u3068\u308a\u3092\u3001\u5236\u9650\u4ed8\u304d\u3067\u8a31\u53ef\u3059\u308b\u4ed5\u7d44\u307f\u300d\u3067\u3059\u3002<\/p>\n<ul>\n<li>\n<p>\u300c\u7570\u306a\u308b\u5834\u6240\u300d\uff1d<strong>\u30aa\u30ea\u30b8\u30f3\u304c\u7570\u306a\u308b<\/strong>\u3053\u3068\u3092\u6307\u3057\u307e\u3059\u3002<br \/>\u203b\u30aa\u30ea\u30b8\u30f3\u3068\u306f\u3001\u300c\u30d7\u30ed\u30c8\u30b3\u30eb\uff08http\/https\uff09\u30fb\u30c9\u30e1\u30a4\u30f3\u540d\u30fb\u30dd\u30fc\u30c8\u756a\u53f7\u300d\u306e\u30bb\u30c3\u30c8\u306e\u3053\u3068\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u305f\u3068\u3048\u3070\u3001\u3042\u306a\u305f\u306eWeb\u30a2\u30d7\u30ea\u304c\u300chttp:\/\/localhost:3000\u300d\u306b\u3042\u308a\u3001\u5916\u90e8API\u304c\u300chttps:\/\/api.example.com\u300d\u306b\u3042\u308b\u5834\u5408\u3001\u3053\u308c\u306f<strong>\u7570\u306a\u308b\u30aa\u30ea\u30b8\u30f3<\/strong>\u3067\u3059\u3002<\/p>\n<p>Web\u30d6\u30e9\u30a6\u30b6\u306f\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u305f\u3081\u306b\u300c\u9055\u3046\u30aa\u30ea\u30b8\u30f3\u304b\u3089\u306e\u30c7\u30fc\u30bf\u53d6\u5f97\u300d\u3092\u5236\u9650\u3057\u307e\u3059\u3002\u3053\u308c\u304c<strong>CORS\u30a8\u30e9\u30fc<\/strong>\u306e\u5143\u3067\u3059\u3002<\/p>\n<hr>\n<h3>\u5177\u4f53\u7684\u306aCORS\u30a8\u30e9\u30fc\u4f8b\u3068\u539f\u56e0<\/h3>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u51fa\u305f\u5834\u5408\u3001\u305d\u308c\u306fCORS\u306b\u95a2\u9023\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre><code>No <span>'Access-Control-Allow-Origin'<\/span> header <span>is<\/span> present <span>on<\/span> the requested resource.<\/code><\/pre>\n<p><strong>\u4e3b\u306aCORS\u30a8\u30e9\u30fc\u306e\u539f\u56e0<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u30b5\u30fc\u30d0\u30fc\u304c\u8a31\u53ef\u3057\u3066\u3044\u306a\u3044<\/strong><br \/>API\u30b5\u30fc\u30d0\u30fc\u5074\u304c\u300c\u3069\u306e\u30aa\u30ea\u30b8\u30f3\uff08\u30a2\u30af\u30bb\u30b9\u5143\uff09\u3092\u8a31\u53ef\u3059\u308b\u304b\u300d\u3092\u30ec\u30b9\u30dd\u30f3\u30b9\u30d8\u30c3\u30c0\u30fc\u3067\u660e\u793a\u3057\u3066\u3044\u306a\u3044\u3068\u3001\u30d6\u30e9\u30a6\u30b6\u304c\u901a\u4fe1\u3092\u30d6\u30ed\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u30d7\u30ea\u30d5\u30e9\u30a4\u30c8\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u5931\u6557\u3057\u3066\u3044\u308b<\/strong><br \/>\u7279\u5b9a\u306e\u6761\u4ef6\uff08\u4f8b\uff1a\u30ab\u30b9\u30bf\u30e0\u30d8\u30c3\u30c0\u30fc\u4ed8\u304d\u306ePOST\uff09\u3067\u306f\u3001<strong>\u4e8b\u524d\u78ba\u8a8d\u306e\u305f\u3081\u306eOPTIONS\u30ea\u30af\u30a8\u30b9\u30c8<\/strong>\uff08\u30d7\u30ea\u30d5\u30e9\u30a4\u30c8\uff09\u304c\u9001\u3089\u308c\u307e\u3059\u3002\u3053\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306b\u5bfe\u3057\u3066\u30b5\u30fc\u30d0\u30fc\u304c\u6b63\u3057\u304f\u5fdc\u7b54\u3057\u306a\u3044\u3068\u3001\u5b9f\u969b\u306e\u901a\u4fe1\u3082\u62d2\u5426\u3055\u308c\u307e\u3059\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8a8d\u8a3c\u60c5\u5831\u4ed8\u304d\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u5236\u9650<\/strong><br \/>\u30af\u30c3\u30ad\u30fc\u3084\u30c8\u30fc\u30af\u30f3\u3092\u542b\u3080\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3059\u308b\u5834\u5408\u3001credentials: &#8216;include&#8217; \u306a\u3069\u306e\u8a2d\u5b9a\u304c\u5fc5\u8981\u3067\u3059\u3002\u3055\u3089\u306b\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u3082 Access-Control-Allow-Credentials: true \u306e\u8a2d\u5b9a\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u30aa\u30ea\u30b8\u30f3\u304c\u4e00\u81f4\u3057\u3066\u3044\u306a\u3044<\/strong><br \/>\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308b\u5143\u306eURL\u3068\u3001API\u306eURL\u306e<strong>\u30c9\u30e1\u30a4\u30f3\u30fb\u30dd\u30fc\u30c8\u30fb\u30d7\u30ed\u30c8\u30b3\u30eb<\/strong>\u304c1\u3064\u3067\u3082\u7570\u306a\u308b\u3068\u3001\u30aa\u30ea\u30b8\u30f3\u304c\u7570\u306a\u308b\u3068\u5224\u65ad\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u304cCORS\u30c1\u30a7\u30c3\u30af\u306e\u5bfe\u8c61\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<\/li>\n<\/ol>\n<hr>\n<h2>\u5bfe\u51e6\u6cd5\u3068\u30b3\u30fc\u30c9\u4f8b<\/h2>\n<h3>&#x2705;\u65b9\u6cd51\uff1a\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u300c\u8a31\u53ef\u300d\u3092\u660e\u793a\u3059\u308b<\/h3>\n<p>\u6700\u3082\u78ba\u5b9f\u306a\u306e\u306f<strong>API\u306e\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u300c\u3053\u306e\u30b5\u30a4\u30c8\u304b\u3089\u306e\u30a2\u30af\u30bb\u30b9\u3092\u8a31\u53ef\u3057\u307e\u3059\u300d\u3068\u8a2d\u5b9a\u3059\u308b\u3053\u3068<\/strong>\u3067\u3059\u3002<\/p>\n<p>Node.js\uff08Express\uff09\u306e\u5834\u5408\uff1a<\/p>\n<pre><code><span>const<\/span> express = <span>require<\/span>(<span>'express'<\/span>);\r\n<span>const<\/span> app = express();\r\n\r\napp.use(<span>(<span>req, res, next<\/span>) =&gt;<\/span> {\r\n  res.header(<span>'Access-Control-Allow-Origin'<\/span>, <span>'http:\/\/localhost:3000'<\/span>); <span>\/\/ \u8a31\u53ef\u3057\u305f\u3044URL<\/span>\r\n  res.header(<span>'Access-Control-Allow-Methods'<\/span>, <span>'GET, POST, PUT, DELETE'<\/span>);\r\n  res.header(<span>'Access-Control-Allow-Headers'<\/span>, <span>'Content-Type'<\/span>);\r\n  next();\r\n});\r\n\r\napp.get(<span>'\/api\/data'<\/span>, <span>(<span>req, res<\/span>) =&gt;<\/span> {\r\n  res.json({ message: <span>'\u30c7\u30fc\u30bf\u53d6\u5f97\u6210\u529f'<\/span> });\r\n});<\/code><\/pre>\n<p><strong>\u88dc\u8db3<\/strong>\uff1a<\/p>\n<ul>\n<li>\n<p>Access-Control-Allow-Origin \u2026 \u3069\u306e\u30aa\u30ea\u30b8\u30f3\u3092\u8a31\u53ef\u3059\u308b\u304b\uff08*\u3082\u53ef\u3002\u305f\u3060\u3057\u5236\u9650\u3042\u308a\uff09<\/p>\n<\/li>\n<li>\n<p>Access-Control-Allow-Credentials \u2026 \u8a8d\u8a3c\u60c5\u5831\uff08\u30af\u30c3\u30ad\u30fc\u306a\u3069\uff09\u4ed8\u304d\u306e\u5834\u5408\u306b\u5fc5\u8981<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h3>\u958b\u767a\u4e2d\u306f\u300c\u30d7\u30ed\u30ad\u30b7\u300d\u3067\u56de\u907f\u3059\u308b<\/h3>\n<p>\u958b\u767a\u4e2d\u306b\u81ea\u5206\u3067\u7ba1\u7406\u3067\u304d\u306a\u3044API\u3092\u4f7f\u3044\u305f\u3044\u5834\u5408\u306f\u3001\u300c\u540c\u4e00\u30aa\u30ea\u30b8\u30f3\u306b\u898b\u305b\u304b\u3051\u308b\u300d\u30d7\u30ed\u30ad\u30b7\u3092\u4f7f\u3046\u306e\u3082\u624b\u3067\u3059\u3002<\/p>\n<p>\u4f8b\uff1aReact\uff08Vite\uff09\u3067\u306e\u8a2d\u5b9a\uff08vite.config.js\uff09<\/p>\n<pre><code><span>server<\/span>: {\r\n  <span>proxy<\/span>: {\r\n    <span>'\/api'<\/span>: {\r\n      target: <span>'https:\/\/api.example.com'<\/span>,\r\n      changeOrigin: true,\r\n      rewrite: path =&gt; path.<span>replace<\/span>(\/^\/api\/, <span>''<\/span>)\r\n    }\r\n  }\r\n}<\/code><\/pre>\n<p>\u4ed5\u7d44\u307f\uff1a<\/p>\n<pre><code>\u3042\u306a\u305f\u306eReact\u30a2\u30d7\u30ea\uff08localhost:<span>3000<\/span>\uff09\r\n      \u2193\r\n\u30ed\u30fc\u30ab\u30eb\u3067 <span>`\/api\/xxx`<\/span> \u306b\u30ea\u30af\u30a8\u30b9\u30c8\r\n      \u2193\r\nVite\u304c\u88cf\u3067 https:<span>\/\/api.example.com \u306b\u8ee2\u9001\uff08CORS\u3092\u56de\u907f\uff09<\/span><\/code><\/pre>\n<hr>\n<h3>fetch\u3067\u8a8d\u8a3c\u4ed8\u304d\u901a\u4fe1\u3092\u884c\u3046\u5834\u5408<\/h3>\n<pre><code>fetch(<span>'https:\/\/api.example.com\/data'<\/span>, {\r\n  <span>method<\/span>: <span>'GET'<\/span>,\r\n  <span>credentials<\/span>: <span>'include'<\/span> <span>\/\/ \u8a8d\u8a3c\u60c5\u5831\u3092\u542b\u3081\u308b\u8a2d\u5b9a<\/span>\r\n})\r\n  .then(<span><span>res<\/span> =&gt;<\/span> res.json())\r\n  .then(<span><span>data<\/span> =&gt;<\/span> <span>console<\/span>.log(data))\r\n  .catch(<span><span>err<\/span> =&gt;<\/span> <span>console<\/span>.error(<span>'\u30a8\u30e9\u30fc\u767a\u751f:'<\/span>, err));<\/code><\/pre>\n<p><strong>\u6ce8\u610f\u70b9<\/strong>\uff1a<\/p>\n<ul>\n<li>\n<p>\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u3082 Access-Control-Allow-Credentials: true \u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<\/li>\n<li>\n<p>\u3053\u306e\u5834\u5408\u3001Access-Control-Allow-Origin \u3092 * \u306b\u3067\u304d\u307e\u305b\u3093\u3002\u30aa\u30ea\u30b8\u30f3\u3092\u660e\u793a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h2>\u307e\u3068\u3081<\/h2>\n<ul>\n<li>\n<p>CORS\u306f\u3001\u7570\u306a\u308b\u30b5\u30a4\u30c8\u9593\u306e\u30c7\u30fc\u30bf\u53d6\u5f97\u3092\u5236\u9650\u3059\u308b\u4ed5\u7d44\u307f\u3002<\/p>\n<\/li>\n<li>\n<p>JavaScript\u3067API\u9023\u643a\u3059\u308b\u5834\u5408\u3001<strong>CORS\u306e\u5236\u7d04\u3067\u901a\u4fe1\u304c\u30d6\u30ed\u30c3\u30af\u3055\u308c\u308b\u3053\u3068\u304c\u3042\u308b<\/strong>\u3002<\/p>\n<\/li>\n<li>\n<p>\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u8a2d\u5b9a or \u958b\u767a\u4e2d\u306f\u30d7\u30ed\u30ad\u30b7\u3067\u5bfe\u5fdc\u3059\u308b\u306e\u304c\u57fa\u672c\u3002<\/p>\n<\/li>\n<li>\n<p>\u8a8d\u8a3c\u60c5\u5831\u4ed8\u304d\u306e\u901a\u4fe1\u306b\u306f\u3001\u8ffd\u52a0\u8a2d\u5b9a\u304c\u5fc5\u8981\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u3010\u5916\u90e8\u30ea\u30f3\u30af\u3011<\/h3>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/HTTP\/CORS\">MDN Web Docs: CORS\uff08\u30af\u30ed\u30b9\u30aa\u30ea\u30b8\u30f3\u30ea\u30bd\u30fc\u30b9\u30b7\u30a7\u30a2\u30ea\u30f3\u30b0\uff09<\/a><\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Fetch_API\">MDN Web Docs: Fetch API<\/a><\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/HTTP\/Headers\/Access-Control-Allow-Origin\">MDN Web Docs: Access-Control-Allow-Origin<\/a><\/p>\n<p><a href=\"https:\/\/expressjs.com\/en\/resources\/middleware\/cors.html\">Express\u516c\u5f0f: CORS\u30df\u30c9\u30eb\u30a6\u30a7\u30a2<\/a><\/p>\n<p><a href=\"https:\/\/vitejs.dev\/config\/server-options.html#server-proxy\">Vite\u516c\u5f0f: server.proxy \u8a2d\u5b9a<\/a><\/p>\n<h3>\u3010\u5185\u90e8\u30ea\u30f3\u30af\u3011<\/h3>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1128\/\">JavaScript\u306eAjax\u5b8c\u5168\u5165\u9580\uff5cfetch\u306e\u4f7f\u3044\u65b9\u30fbCORS\u30a8\u30e9\u30fc\u306e\u5bfe\u51e6\u30fb\u975e\u540c\u671f\u901a\u4fe1\u306e\u57fa\u672c\u3092\u89e3\u8aac<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1109\/\">JavaScript\u3067\u3088\u304f\u767a\u751f\u3059\u308b\u30a8\u30e9\u30fc\u300cReferenceError\u300d\u300cSyntaxError\u300d\u306a\u3069\u306e\u539f\u56e0\u3068\u5bfe\u7b56\u3092\u5177\u4f53\u4f8b\u4ed8\u304d\u3067\u89e3\u8aac<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/894\/\">\u3010\u521d\u5fc3\u8005\u3067\u3082\u7c21\u5358\uff01\u3011JavaScript\u3067\u59cb\u3081\u308b\u7c21\u5358\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\uff5c\u97f3\u58f0\u6587\u5b57\u8d77\u3053\u3057\u30c4\u30fc\u30eb\u4f5c\u6210\u30ac\u30a4\u30c9<\/a><\/p>\n<\/div>\n","protected":false},"featured_media":1206,"template":"","_links":{"self":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route\/1205"}],"collection":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route"}],"about":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/types\/route"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media\/1206"}],"wp:attachment":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media?parent=1205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}