teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

just exempt csrf

2020/07/23 09:41

投稿

YufanLou
YufanLou

スコア466

answer CHANGED
@@ -21,6 +21,14 @@
21
21
  )
22
22
  ```
23
23
 
24
+ 追記:そうしてもCSRFを踏んだら、Django側その関数に @csrf_exempt でマークしてください
25
+
26
+ ```python
27
+ @csrf_exempt
28
+ def check():
29
+ pass
30
+ ```
31
+
24
32
  ---
25
33
 
26
34
  AkitoshiManabeさんの言う通り、`http://127.0.0.1:8080` と `http://localhost:8080`との違いで Cross Origin エラーが出てます。

2

fix proxy port number

2020/07/23 09:41

投稿

YufanLou
YufanLou

スコア466

answer CHANGED
@@ -4,7 +4,7 @@
4
4
  // vue.config.js
5
5
  module.exports = {
6
6
  devServer: {
7
- proxy: 'http://localhost:4000'
7
+ proxy: 'http://localhost:8000'
8
8
  }
9
9
  }
10
10
  ```

1

use proxy to access backend

2020/07/23 07:37

投稿

YufanLou
YufanLou

スコア466

answer CHANGED
@@ -1,3 +1,28 @@
1
+ 追記:[Vue 公式ドキュメント](https://cli.vuejs.org/config/#devserver-proxy)によって、フロントエンドとバックエンドのサーバーが同一ではない場合、設定の devServer.proxy にバックエンドサーバーのアドレスを入れれば、フロントエンドで知らないリクエストを全部バックエンドに渡せます。
2
+
3
+ ```javascript
4
+ // vue.config.js
5
+ module.exports = {
6
+ devServer: {
7
+ proxy: 'http://localhost:4000'
8
+ }
9
+ }
10
+ ```
11
+
12
+ そうしたら、リクエストの際にアドレスを明言する必要がなくなります。
13
+
14
+ ```javascript
15
+ axios.post(
16
+ '/polls/check/',
17
+ {
18
+ name:this.name,
19
+ comment:this.comment
20
+ },
21
+ )
22
+ ```
23
+
24
+ ---
25
+
1
26
  AkitoshiManabeさんの言う通り、`http://127.0.0.1:8080` と `http://localhost:8080`との違いで Cross Origin エラーが出てます。
2
27
 
3
28
  こちらのコードから推測できる誤解を解けておきましょう: