高级前端
http
【Q358】什么情况下会发送 OPTIONS 请求

什么情况下会发送 OPTIONS 请求

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 363 (opens in a new tab)

Author 回答者: nextprops (opens in a new tab)

搬运地址 (opens in a new tab) 1:请求的方法不是GET/HEAD/POST 2:POST请求的Content-Type 异常 3:请求设置了自定义的header字段

Author 回答者: shfshanyue (opens in a new tab)

当一个请求跨域且不是简单请求时就会发送 OPTIONS 请求

满足以下条件就是一个简单请求:

  1. Method: 请求的方法是 GETPOSTHEAD
  2. Header: 请求头是 Content-TypeAccept-LanguageContent-Language
  3. Content-Type: 请求类型是 application/x-www-form-urlencodedmultipart/form-datatext/plain

而在项目中常见的 Content-Type: application/jsonAuthorization: <token> 为典型的非简单请求,在发送请求时往往会带上 Options

更详细内容请参考 CORS - MDN (opens in a new tab)