用fetch向后端发送post请求,为什么会产生两个请求(OPTIONS和POST)记录?

itwriter 发布于 2017/03/01 14:18
阅读 887
收藏 0

比方说发送post请求“/createSth”,在控制台能看到两个url一致的请求。

第一个无body、无响应、以OPTIONS方式发送

第二个就是正常的有参数body、有response、post方式提交的请求。

加载中
0
itwriter
itwriter

fetch的实现机制导致的结果。当发生跨域请求时,fetch会先发送一个OPTIONS请求,来确认服务器是否允许接受请求。服务器同意后,才会发送真正的请求。

Additionally, for HTTP request methods that can cause side-effects on user data (in particular, for HTTP methods other than GET, or for POST usage with certain MIME types), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method. Servers can also notify clients whether "credentials" (including Cookies and HTTP Authentication data) should be sent with requests.

返回顶部
顶部