为什么 http 请求从客户端发送两次到服务器( OPTION 和 POST )

Why http request send twice from client to server ( OPTION and POST )

当我点击提交按钮时,http 请求从客户端发送到服务器两次。 第一个是选项,另一个是 POST 方法。

登录表单

HTTP 请求

根据 Mozilla Developer Network:

Preflighted requests

Unlike simple requests (discussed above), "preflighted" requests first send an HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send. Cross-site requests are preflighted like this since they may have implications to user data. In particular, a request is preflighted if:

It uses methods other than GET 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, or text/plain, e.g. if the POST request sends an XML payload to the server using application/xml or 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)