1. 程式人生 > >fetch進行post請求為什麼會首先發一個options 請求?

fetch進行post請求為什麼會首先發一個options 請求?

不僅僅是fetch,只要你滿足以下幾種情況,都會去發起一個 Preflighted requests,也就是options請求,參見上面連結。

  • It uses methods other than GET, HEAD or POST. Also, if POST is used to send request data with a Content-Type other than application/x-www-form-urlencoded, multipart/form-data, ortext/plain, e.g. if the POST request sends an XML payload to the server using application/xmlor text/xml, then the request is
    preflighted.
  • It sets custom headers in the request (e.g. the request uses a header such as X-PINGOTHER)


你看看你是不是觸發這幾個條件中的其中一種了吧,瀏覽器在發起跨域請求的時候會帶著一個`Origin` header,那麼其實就是個custom headers,那麼也就會先觸發一個Preflighted requests,
Fetch Standard 也有提到。