gpt4 book ai didi

javascript - 飞行前 OPTIONS 请求 CORS

转载 作者:行者123 更新时间:2023-11-28 18:42:06 26 4
gpt4 key购买 nike

我在开发中使用这样的中间调用cors

app.use(cors({origin: 'http://localhost:8100'}));

对于每个路由请求,我开始看到 2 个请求:

enter image description here

这种情况在生产中还会发生吗?这是个问题吗?

最佳答案

是的,这种情况还会继续发生。不,这不是问题,除非您创建了与其冲突的路由。

浏览器使用 OPTIONS 请求来检测服务器对该特定端点的 CORS 支持。

来自Mozilla MDN :

... 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.

...

Unlike simple requests (discussed above), "preflighted" requests first send an HTTP request by the OPTIONS method 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.

关于javascript - 飞行前 OPTIONS 请求 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35978335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com