Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ブラウザでのクロスドメインのHTTPリクエスト時にはpreflightリクエストが入る #1

Open
ghost opened this issue Oct 17, 2018 · 0 comments
Labels

Comments

@ghost
Copy link

ghost commented Oct 17, 2018

preflightリクエスト

CORSのHTTPリクエストを行う場合、ある特定条件下ではpreflightリクエストというものが実際のリクエストの前に行われる。(つまり計2回通信する)

特定条件とは

以下の条件のどれかが満たされたとき、preflightリクエストを行う

  • メソッドがGET, POST, HEAD以外である
  • ヘッダにAccept, Accept-Language, Content-Language, Content-Type以外のフィールドが含まれる
  • Content-Typeが application/x-www-form-urlencoded, multipart/form-data, text/plainの以外である

流れ

preflightリクエストは以下のようなリクエスト、

  • メソッドはOPTIONS
  • リクエストヘッダにOrigin, Access-Control-Request-Method, (あれば)
    Access-Control-Request-Headers など、「こういう内容で通信しますよ」とサーバに事前に知らせる

これに対するレスポンスは、

  • Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers などを返し、「こういう内容を許可しますよ」と示す。

preflightリクエストとそのレスポンスは対になるようなヘッダを持つ。

ここまでやって許可されてから、やっと実際の通信が行われる。

参考
https://dev.classmethod.jp/etc/about-cors/

@ghost ghost added HTTP javascript and removed javascript labels Oct 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants