{"id":2757,"date":"2021-03-19T14:48:43","date_gmt":"2021-03-19T05:48:43","guid":{"rendered":"https:\/\/qol-kk.com\/wp2\/?p=2757"},"modified":"2021-03-19T15:03:40","modified_gmt":"2021-03-19T06:03:40","slug":"post-2757","status":"publish","type":"post","link":"https:\/\/qol-kk.com\/wp2\/blog\/2021\/03\/19\/post-2757\/","title":{"rendered":"Vue.js\uff1aDjango\u306eSPA\u5316\u3067\u8a18\u9332\u3057\u3066\u304a\u3044\u305f\u65b9\u304c\u826f\u3044\u3068\u611f\u3058\u305fError\u3068\u6539\u5584\u65b9\u6cd5"},"content":{"rendered":"<h2>Vue.js\uff1aDjango\u306eSPA\u5316\u3067\u8a18\u9332\u3057\u3066\u304a\u3044\u305f\u65b9\u304c\u826f\u3044\u3068\u611f\u3058\u305fError\u3068\u6539\u5584\u65b9\u6cd5<\/h2>\n<p>\u6628\u65e5\u306e\u8a18\u4e8b\u306e\u7d9a\u304d\u306e\u3088\u3046\u306a\u5185\u5bb9\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\nDocker\u3067\u3010 Django \u00d7 Vue \u3011 \u306eSPA\u74b0\u5883\u3092\u69cb\u7bc9\u3059\u308b\u969b\u306b\u51fa\u3066\u304d\u305fAPI\u7d61\u307f\u306e\u30a8\u30e9\u30fc\u3092\u307e\u3068\u3081\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p>Docker\u3092\u4f5c\u308a\u5909\u3048\u3066\u30ea\u30d3\u30eb\u30c9\u3057\u3066\u3092\u7e70\u308a\u8fd4\u3057\u305f\u969b\u306b\u591a\u51fa\u3057\u305f\u30a8\u30e9\u30fc\u304c\u4e0b\u306e2\u3064\u3067\u3059\u3002<\/p>\n<div class=\"borderBox\">\n<ol>\n<li>cannot read property 'get' of undefined axios<\/li>\n<li>Access to XMLHttpRequest at 'API\u306eURL' from origin 'Vue\u306eURL' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.<\/li>\n<\/ol>\n<\/div>\n<p>\u5176\u3005\u306b\u5bfe\u3057\u3066\u539f\u56e0\u3068\u306a\u3063\u3066\u3044\u305f\u500b\u6240\u3092\u8a18\u8f09\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u203b\u524d\u63d0\u6761\u4ef6\u3068\u3057\u3066\u3010Vue\u304c\u8d77\u52d5\u3057\u3066\u3044\u3066API\u3092\u53d7\u3051\u53d6\u308c\u306a\u3044\u3011\u72b6\u614b\u3092\u6539\u5584\u3059\u308b\u30dd\u30a4\u30f3\u30c8\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u300e\u30b3\u30f3\u30c6\u30ca\u304c\u52d5\u304b\u306a\u3044\u300f\u300eVue\u304c\u8d77\u52d5\u3057\u306a\u3044\u300f\u306b\u306f\u30de\u30c3\u30c1\u3057\u306a\u3044\u8a18\u4e8b\u3067\u3059\u3002<\/p>\n<h3>cannot read property 'get' of undefined axios<\/h3>\n<div class=\"paperBox\">\n<p>\u672a\u5b9a\u7fa9\u306e\u30a2\u30af\u30b7\u30aa\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u300cget\u300d\u304c\u8aad\u3081\u306a\u3044<\/p>\n<\/div>\n<p>\u7ffb\u8a33\u6a5f\u306b\u639b\u3051\u308b\u3068\u4e0a\u306e\u610f\u5473\u3060\u3068\u8a00\u3046\u4e8b\u304c\u308f\u304b\u308a\u307e\u3059\u3002<br \/>\n\u3010 axios \u3011\u306fVue\u3067API\u3092\u30ea\u30af\u30a8\u30b9\u30c8\u3059\u308b\u30c4\u30fc\u30eb\u3067\u3059\u3002\u5148\u8f29\u65b9\u306e\u8a00\u8449\u3092\u501f\u308a\u308b\u3068\u4e0b\u306e\u5185\u5bb9\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<div class=\"borderBox\">Ajax\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308b\u305f\u3081\u306epromise\u30d9\u30fc\u30b9\u306eHTTP\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3067\u3001\u7c21\u5358\u3067\u8c4a\u5bcc\u306aAPI\u3092\u63d0\u4f9b\u3057\u3066\u304f\u308c\u308b\u3082\u306e<\/div>\n<h4>undefined axios\u306e\u539f\u56e0\u306fmains.js\u306e\u8a2d\u5b9a\u30df\u30b9<\/h4>\n<p>\u51fa\u529bvue\u30d5\u30a1\u30a4\u30eb\u5185\u306e\u3010this.$axios.get(API.url)\u3011\u304c\u898b\u3048\u3066\u3044\u306a\u3044\u304b\u3089\u767a\u751f\u3057\u3066\u3044\u308b\u30a8\u30e9\u30fc\u3067\u3059\u3002<\/p>\n<p>\u300cacios\u304c\u672a\u5b9a\u7fa9\u300d\u3068\u8a00\u3063\u3066\u3044\u308b\u308f\u3051\u3067\u3059\u304b\u3089\u3001mains.js\u306e\u8a2d\u5b9a\u306b\u4e0b\u306e\u30b3\u30fc\u30c9\u304c\u542b\u307e\u308c\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\nUse\uff08import\uff09\u5fd8\u308c\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"\">import axios from 'axios'<\/pre>\n<h4>API\u306eURL\u304c\u5b58\u5728\u3057\u306a\u3044\u3068\u304d\u306f404\u30a8\u30e9\u30fc<\/h4>\n<p>\u300e\u300cget\u300d\u304c\u8aad\u3081\u306a\u3044\u300f\u3068\u8a00\u308f\u308c\u308b\u3068\u30d5\u30ed\u30f3\u30c8\u5074\u3067\u306f\u306a\u304f\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u5074\u306b\u554f\u984c\u304c\u3042\u308b\u3068\u8003\u3048\u3066\u3057\u307e\u3044\u304c\u3061\u3067\u3059\u3002<br \/>\n\u5b9f\u969b\u3001\u79c1\u3082Backend\u306eURL\u30ea\u30b9\u30c8\u3092\u78ba\u8a8d\u3057\u305f\u308a\u3057\u307e\u3057\u305f\u3002<br \/>\n\u3067\u3082\u3001\u30ea\u30af\u30a8\u30b9\u30c8URL\u304c\u7570\u306a\u3063\u3066\u3044\u308b\u5834\u5408\u306e\u30a8\u30e9\u30fc\u306f404\u30a8\u30e9\u30fc\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">Uncaught (in createError ... promise) Error: Request failed with status code 404<\/pre>\n<p>\u7ffb\u8a33\u6a5f\u306e\u548c\u8a33\u306b\u6d41\u3055\u308c\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>Access to XMLHttpRequest at ... has been blocked by CORS policy: ...<\/h3>\n<div class=\"paperBox\">\n<p>\u30aa\u30ea\u30b8\u30f3\u300eVue\u306eURL\u300f\u304b\u3089\u300eAPI\u306eURL\u300f\u306eXMLHttpRequest\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306f\u3001CORS \u30dd\u30ea\u30b7\u30fc\u306b\u3088\u3063\u3066\u30d6\u30ed\u30c3\u30af\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u30ea\u30af\u30a8\u30b9\u30c8\u3055\u308c\u305f\u30ea\u30bd\u30fc\u30b9\u306b'Access-Control-Allow-Origin'\u30d8\u30c3\u30c0\u30fc\u304c\u5b58\u5728\u3057\u307e\u305b\u3093\u3002<\/p>\n<\/div>\n<p>\u3053\u308c\u306f\u30d5\u30ed\u30f3\u30c8\u3068\u30d0\u30c3\u30af\u306eURL(\u30dd\u30fc\u30c8\u542b\u3081)\u304c\u7570\u306a\u308b\u305f\u3081\u306b\u767a\u751f\u3057\u3066\u3044\u308b\u30a8\u30e9\u30fc\u3067\u3059\u3002<br \/>\nSPA\u306e\u69cb\u7bc9\u3067\u306f\u3088\u304f\u51fa\u3057\u3066\u3057\u307e\u3046\u30a8\u30e9\u30fc\u306e\u69d8\u3067\u53c2\u8003\u8cc7\u6599\u3082\u6ca2\u5c71\u3042\u308a\u307e\u3057\u305f\u3002<br \/>\n<a href=\"https:\/\/qiita.com\/Ryoma0413\/items\/c41d10d2e6e2a420c3cf\">\u3053\u306e\u8a18\u4e8b<\/a>\u304c\u308f\u304b\u308a\u3084\u3059\u304b\u3063\u305f\u3067\u3059\u3002<\/p>\n<p>\u5177\u4f53\u7684\u306a\u56de\u907f\u65b9\u6cd5\u306fvue.config.js\u3092Vue\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u76f4\u4e0b\uff08package.js\u3068\u540c\u3058\u5834\u6240\uff09\u306b\u4f5c\u6210\u3057\u3053\u3053\u306b\u30d7\u30ed\u30ad\u30b7\u3092\u8a2d\u5b9a\u3059\u308b\u4e8b\u3002\u79c1\u306f\u4e0b\u306e\u30b3\u30fc\u30c9\u3060\u3051\u3067\u6539\u5584\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true\">#vue.config.js\r\nmodule.exports = {\r\n  devServer: {\r\n    proxy: 'API\u306eURL:\u30dd\u30fc\u30c8'\r\n  }\r\n};<\/pre>\n<h4>vue.config.js\u3092\u8a2d\u7f6e\u3059\u308b\u969b\u306e\u6ce8\u610f\u70b9<\/h4>\n<div class=\"pointBox\">\n<p>vue.config.js\u306fVue\u3092\u518d\u8d77\u52d5\u3057\u306a\u3044\u3068\u9069\u7528\u3055\u308c\u306a\u3044<\/p>\n<\/div>\n<p>vue\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u7d20\u306e\u72b6\u614b\u306e\u6642\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306eRoot\uff08package.js\u3068\u540c\u3058\u5834\u6240\uff09\u306bvue.config.js\u306f\u5b58\u5728\u3057\u307e\u305b\u3093\u3002\u305d\u306e\u305f\u3081\u3001\u65b0\u305f\u306b\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u3067\u3001\u65b0\u305f\u306b\u4f5c\u6210\u3057\u3066\u3082\u305d\u308c\u3060\u3051\u3067\u306f\u9069\u7528\u3055\u308c\u307e\u305b\u3093\u3002Vue\u30b5\u30fc\u30d3\u30b9\u306e\u518d\u8d77\u52d5\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<p>Vue\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\/src\/components\u5185\u306eVue\u30d5\u30a1\u30a4\u30eb\u306a\u3093\u3066\u66f8\u3044\u305f\u305d\u3070\u304b\u3089WEB\u753b\u9762\u306b\u5909\u5316\u304c\u3042\u308b\u306e\u3067\u30b3\u30f3\u30d5\u30a3\u30b0\u3082\u305d\u3046\u306a\u308b\u3068\u601d\u3044\u8fbc\u3093\u3067\u306f\u3044\u3051\u307e\u305b\u3093\u3002\u79c1\u306f\u3053\u308c\u3067\u304b\u306a\u308a\u306e\u6642\u9593\u3092\u4f7f\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002\u5909\u66f4\u3057\u3066\u3082\u30ca\u30cb\u30e2\u5909\u308f\u3089\u306a\u3044\u306e\u3067\u4ed6\u306b\u554f\u984c\u304c\u3042\u308b\u306e\u304b\u3068\u601d\u3044\u3060\u3057\u305f\u308a\u3057\u3066\u3057\u307e\u3044\u304c\u3061\u3067\u3059\u304c\u3001\u5fc5\u305aVue\u306e\u518d\u8d77\u52d5\u304c\u5fc5\u8981\u3067\u3059\u3002\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>CORS \u30dd\u30ea\u30b7\u30fc\u9055\u53cd\u3063\u3066\u306a\u306b\uff1f<\/h3>\n<p>\u30af\u30ed\u30b9\u30c9\u30e1\u30a4\u30f3\u30a8\u30e9\u30fc\u3068\u3082\u3044\u308f\u308c\u308b\u30e1\u30b8\u30e3\u30fc\u306a\u30a8\u30e9\u30fc\u539f\u56e0\u3067\u3059\u3002<br \/>\nAPI\u3067\u306f\u30c9\u30e1\u30a4\u30f3\u3084\u30dd\u30fc\u30c8\u3092\u307e\u305f\u3044\u3067\u3044\u308b\u4e8b\u304c\u6b86\u3069\u3060\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u81ea\u5206\u304b\u3089\u81ea\u5206\u306b\u5bfe\u3057\u3066AP\u3044\u3067\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u30b1\u30fc\u30b9\u304c\u30bc\u30ed\u3068\u306f\u8a00\u3044\u307e\u305b\u3093\u304c\u3001\u591a\u304f\u306e\u5834\u5408\u306f\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<div class=\"paperBox\">\n<ul>\n<li>Frontend\uff1a http:\/\/host:8080<\/li>\n<li>Backend\uff1ahttp:\/\/host:8000<\/li>\n<\/ul>\n<\/div>\n<p>\u30dd\u30fc\u30c8\u756a\u53f7\u306f\u69d8\u3005\u3060\u3068\u601d\u3044\u307e\u3059\u304c\u3001Front\u3068Back\u3067\u306f\u540c\u4e00\u30c9\u30e1\u30a4\u30f3\u3067\u3042\u3063\u3066\u3082\u5225\u30dd\u30fc\u30c8\u3067\u69cb\u7bc9\u3057\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u6642\u3001\u3061\u3083\u3093\u3068\u3057\u305fAPI\u3067\u306fCORS\u306e\u898f\u7d04\u306b\u9055\u53cd\u3057\u306a\u3044\u3088\u3046\u306b Access-Control-Allow-Origin \u3092\u30d8\u30c3\u30c0\u30fc\u5185\u306b\u683c\u7d0d\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3092\u8a31\u53ef\u3059\u308b\u30a2\u30c9\u30ec\u30b9\u3092\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">Access-Control-Allow-Origin: 'https:\/\/example.com'<\/pre>\n<p>\u4f8b\u3048\u3070\u4e0a\u306e\u69d8\u306a\u8a18\u8f09\u304c\u3042\u308c\u3070 https:\/\/example.com \u304b\u3089\u306e\u63a5\u7d9a\u306f\u8a31\u53ef\u3059\u308b\u3088\u3068\u306e\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u30a2\u30af\u30bb\u30b9\u8a31\u53ef\u3057\u3066\u3044\u306a\u3044URL\u304b\u3089API\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u5c4a\u3044\u305f\u5834\u5408\u300c\u5c55\u958b\u3067\u304d\u307e\u305b\u3093\u300d\u3068\u51e6\u7406\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002<br \/>\n\u4e00\u898b\u3055\u3093\u304a\u65ad\u308a\u306e\u304a\u5e97\u306b\u5165\u3063\u3066\u304d\u305f\u65b0\u898f\u5ba2\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u3059\u306d\u3002<\/p>\n<p>\u4eca\u56de\u3010vue.config.js\u3011\u306b\u8a18\u8f09\u3057\u305f\u30b3\u30fc\u30c9\u306f\u3053\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u53d7\u3051\u53d6\u308c\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>\u307e\u3068\u3081<\/h3>\n<p>JQuery\u3067\u66f8\u304f\u3053\u3068\u304c\u591a\u304b\u3063\u305f\u306e\u3067Vue\u306b\u306f\u307e\u3060\u6163\u308c\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u89e6\u3063\u3066\u307f\u308b\u3068\u9762\u767d\u3044\u3067\u3059\u306d\u3002<\/p>\n<p>\u81ea\u7531\u306b\u66f8\u3044\u3066\u304d\u305f\u8eab\u3068\u3057\u3066\u306f\u30eb\u30fc\u30eb\u306b\u624b\u3053\u305a\u308b\u6240\u306f\u3042\u308a\u307e\u3059\u3051\u3069\u300c\u6163\u308c\u308b\u3068\u65e9\u3044\u3093\u3060\u308d\u3046\u306a\u3041\u300d\u3068\u5b9f\u611f\u3057\u3066\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vue.js\uff1aDjango\u306eSPA\u5316\u3067\u8a18\u9332\u3057\u3066\u304a\u3044\u305f\u65b9\u304c\u826f\u3044\u3068\u611f\u3058\u305fError\u3068\u6539\u5584\u65b9\u6cd5 \u6628\u65e5\u306e\u8a18\u4e8b\u306e\u7d9a\u304d\u306e\u3088\u3046\u306a\u5185\u5bb9\u306b\u306a\u308a\u307e\u3059\u3002 Docker\u3067\u3010 Django \u00d7 Vue \u3011 \u306eSPA\u74b0\u5883\u3092\u69cb\u7bc9\u3059\u308b\u969b\u306b\u51fa\u3066\u304d\u305fAPI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"vkexunit_cta_each_option":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[36,75],"tags":[64,814,498,365,967,966,969,58],"class_list":["post-2757","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-memorandum","category-code-of-serverside","tag-api","tag-django","tag-docker","tag-javascript","tag-spa","tag-vue-js","tag-969","tag-58"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/qol-kk.com\/wp2\/wp-content\/uploads\/2021\/03\/vuejs.png","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/2757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/comments?post=2757"}],"version-history":[{"count":0,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/2757\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media\/2762"}],"wp:attachment":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media?parent=2757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/categories?post=2757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/tags?post=2757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}