{"id":638,"date":"2026-08-01T15:04:05","date_gmt":"2026-08-01T06:04:05","guid":{"rendered":"https:\/\/www.marchboy.xyz\/wordpress\/archives\/638"},"modified":"2026-08-01T15:04:05","modified_gmt":"2026-08-01T06:04:05","slug":"web%e3%81%aecors%e3%81%a8%e3%81%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.marchboy.xyz\/wordpress\/archives\/638","title":{"rendered":"Web\u306eCORS\u3068\u306f\uff1f"},"content":{"rendered":"<h2>Web\u306eCORS\u3068\u306f\uff1f<\/h2>\n<p>Cross-Origin Resource Sharing\uff08CORS\uff09\u306f\u3001Web\u30d6\u30e9\u30a6\u30b6\u304c\u7570\u306a\u308b\u30aa\u30ea\u30b8\u30f3\uff08\u30c9\u30e1\u30a4\u30f3\u3001\u30d7\u30ed\u30c8\u30b3\u30eb\u3001\u30dd\u30fc\u30c8\uff09\u304c\u6301\u3064\u30ea\u30bd\u30fc\u30b9\u3078\u5b89\u5168\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u4ed5\u7d44\u307f\u3067\u3059\u3002\u30d6\u30e9\u30a6\u30b6\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u540c\u4e00\u751f\u6210\u5143\u30dd\u30ea\u30b7\u30fc\uff08Same-Origin Policy\uff09\u3092\u9069\u7528\u3057\u3001\u7570\u306a\u308b\u30aa\u30ea\u30b8\u30f3\u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306f\u30d6\u30ed\u30c3\u30af\u3057\u307e\u3059\u3002CORS\u306f\u30b5\u30fc\u30d0\u5074\u304c\u7279\u5b9a\u306e\u30d8\u30c3\u30c0\u30fc\u3092\u8fd4\u3059\u3053\u3068\u3067\u3001\u30d6\u30e9\u30a6\u30b6\u306b\u300c\u3053\u306e\u30ea\u30bd\u30fc\u30b9\u306f\u8a31\u53ef\u3055\u308c\u305f\u30aa\u30ea\u30b8\u30f3\u304b\u3089\u306e\u30a2\u30af\u30bb\u30b9\u3092\u8a8d\u3081\u308b\u300d\u3053\u3068\u3092\u4f1d\u3048\u307e\u3059\u3002<\/p>\n<h3>\u540c\u4e00\u751f\u6210\u5143\u30dd\u30ea\u30b7\u30fc<\/h3>\n<p>\u540c\u4e00\u751f\u6210\u5143\u30dd\u30ea\u30b7\u30fc\u306f\u3001Web\u30da\u30fc\u30b8\u304c\u8aad\u307f\u8fbc\u307e\u308c\u305f\u30aa\u30ea\u30b8\u30f3\u3068\u7570\u306a\u308b\u30aa\u30ea\u30b8\u30f3\u306e\u30ea\u30bd\u30fc\u30b9\uff08\u4f8b: API\u3001\u753b\u50cf\u3001\u30b9\u30af\u30ea\u30d7\u30c8\uff09\u3078\u306e\u8aad\u307f\u53d6\u308a\u64cd\u4f5c\u3092\u5236\u9650\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u60aa\u610f\u306e\u3042\u308b\u30b5\u30a4\u30c8\u304c\u30e6\u30fc\u30b6\u30fc\u306e\u8a8d\u8a3c\u60c5\u5831\u3092\u4f7f\u3063\u3066\u5225\u30b5\u30a4\u30c8\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u3092\u9632\u6b62\u3057\u307e\u3059\u3002<\/p>\n<h3>CORS\u304c\u89e3\u6c7a\u3059\u308b\u8ab2\u984c<\/h3>\n<p>\u958b\u767a\u8005\u304c\u5916\u90e8API\u3084\u30de\u30a4\u30af\u30ed\u30b5\u30fc\u30d3\u30b9\u9593\u3067\u30c7\u30fc\u30bf\u3092\u3084\u308a\u53d6\u308a\u3057\u305f\u3044\u5834\u5408\u3001\u540c\u4e00\u751f\u6210\u5143\u30dd\u30ea\u30b7\u30fc\u304c\u969c\u58c1\u3068\u306a\u308a\u307e\u3059\u3002CORS\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b7\u30ca\u30ea\u30aa\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\uff08\u4f8b: https:\/\/app.example.com\uff09\u304b\u3089\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\uff08\u4f8b: https:\/\/api.example.com\uff09\u3078\u306e AJAX \u30ea\u30af\u30a8\u30b9\u30c8<\/li>\n<li>\u30b5\u30fc\u30c9\u30d1\u30fc\u30c6\u30a3 CDN \u304b\u3089\u306e\u30d5\u30a9\u30f3\u30c8\u3084\u753b\u50cf\u306e\u53d6\u5f97<\/li>\n<li>\u30e2\u30d0\u30a4\u30eb\u30a2\u30d7\u30ea\u304c WebView \u3092\u4ecb\u3057\u3066 Web API \u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5834\u5408<\/li>\n<\/ul>\n<h3>\u4e3b\u8981\u306a CORS \u30d8\u30c3\u30c0\u30fc<\/h3>\n<ul>\n<li><strong>Access-Control-Allow-Origin<\/strong> \u2013 \u8a31\u53ef\u3059\u308b\u30aa\u30ea\u30b8\u30f3\uff08&#8221;*&#8221; \u306f\u5168\u30aa\u30ea\u30b8\u30f3\uff09<\/li>\n<li><strong>Access-Control-Allow-Methods<\/strong> \u2013 \u8a31\u53ef\u3059\u308b HTTP \u30e1\u30bd\u30c3\u30c9\uff08GET,POST \u306a\u3069\uff09<\/li>\n<li><strong>Access-Control-Allow-Headers<\/strong> \u2013 \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u9001\u4fe1\u3067\u304d\u308b\u30ab\u30b9\u30bf\u30e0\u30d8\u30c3\u30c0\u30fc<\/li>\n<li><strong>Access-Control-Allow-Credentials<\/strong> \u2013 \u30af\u30c3\u30ad\u30fc\u3084\u8a8d\u8a3c\u60c5\u5831\u3092\u542b\u3081\u308b\u304b\u3069\u3046\u304b\uff08true\uff0ffalse\uff09<\/li>\n<li><strong>Access-Control-Max-Age<\/strong> \u2013 \u30d7\u30ea\u30d5\u30e9\u30a4\u30c8\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\u3059\u308b\u79d2\u6570<\/li>\n<\/ul>\n<h3>\u7c21\u5358\u306a\u5b9f\u88c5\u4f8b\uff08Node.js \/ Express\uff09<\/h3>\n<pre><code>\/\/ Node.js (Express) \u306e\u4f8b\nconst express = require('express');\nconst app = express();\n\napp.use((req, res, next) => {\n  \/\/ \u3059\u3079\u3066\u306e\u30aa\u30ea\u30b8\u30f3\u3092\u8a31\u53ef\uff08\u672c\u756a\u74b0\u5883\u3067\u306f\u7279\u5b9a\u306e\u30aa\u30ea\u30b8\u30f3\u306b\u7d5e\u308b\uff09\n  res.setHeader('Access-Control-Allow-Origin', '*');\n  res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');\n  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');\n  \/\/ \u30d7\u30ea\u30d5\u30e9\u30a4\u30c8\u30ea\u30af\u30a8\u30b9\u30c8\uff08OPTIONS\uff09\u306e\u5834\u5408\u306f\u3059\u3050\u306b 204 \u3092\u8fd4\u3059\n  if (req.method === 'OPTIONS') {\n    return res.sendStatus(204);\n  }\n  next();\n});\n\napp.get('\/api\/data', (req, res) => {\n  res.json({ message: 'Hello CORS' });\n});\n\napp.listen(3000, () => console.log('Server running on port 3000'));\n<\/code><\/pre>\n<h3>\u307e\u3068\u3081<\/h3>\n<p>CORS \u306f\u300c\u5b89\u5168\u306b\u300d\u30af\u30ed\u30b9\u30aa\u30ea\u30b8\u30f3\u901a\u4fe1\u3092\u53ef\u80fd\u306b\u3059\u308b\u30d6\u30e9\u30a6\u30b6\u6a19\u6e96\u306e\u4ed5\u7d44\u307f\u3067\u3059\u3002\u30b5\u30fc\u30d0\u5074\u3067\u9069\u5207\u306a\u30d8\u30c3\u30c0\u30fc\u3092\u8a2d\u5b9a\u3057\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u3067\u306f\u5fc5\u8981\u306b\u5fdc\u3058\u3066 <strong>credentials<\/strong> \u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30e2\u30c0\u30f3\u306a\u5206\u6563\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Web\u306eCORS\u3068\u306f\uff1f Cross-Origin Resource Sharing\uff08CORS\uff09\u306f\u3001Web\u30d6\u30e9\u30a6\u30b6\u304c\u7570\u306a\u308b\u30aa\u30ea\u30b8\u30f3\uff08\u30c9\u30e1\u30a4\u30f3\u3001\u30d7\u30ed\u30c8\u30b3\u30eb\u3001\u30dd\u30fc\u30c8\uff09\u304c\u6301\u3064\u30ea\u30bd\u30fc\u30b9\u3078\u5b89\u5168\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u4ed5\u7d44\u307f\u3067\u3059\u3002\u30d6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-638","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/www.marchboy.xyz\/wordpress\/wp-json\/wp\/v2\/posts\/638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marchboy.xyz\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marchboy.xyz\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marchboy.xyz\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marchboy.xyz\/wordpress\/wp-json\/wp\/v2\/comments?post=638"}],"version-history":[{"count":0,"href":"https:\/\/www.marchboy.xyz\/wordpress\/wp-json\/wp\/v2\/posts\/638\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.marchboy.xyz\/wordpress\/wp-json\/wp\/v2\/media?parent=638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marchboy.xyz\/wordpress\/wp-json\/wp\/v2\/categories?post=638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marchboy.xyz\/wordpress\/wp-json\/wp\/v2\/tags?post=638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}