开发者社区> 问答> 正文

用fetch向后端发送post请求,为什么会产生两个请求(OPTIONS和POS?400报错

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

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

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

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

展开
收起
爱吃鱼的程序员 2020-06-03 17:08:53 774 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    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.

    2020-06-03 20:35:41
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
十分钟上线-使用函数计算构建支付宝小程序服务 立即下载
机器学习在互联网后端技术中的应用 立即下载
低代码开发师(初级)实战教程 立即下载