javascript - 飞行前 OPTIONS 请求 CORS

标签 javascript angularjs node.js cors

我在开发中使用这样的中间调用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/

相关文章:

javascript - Twitter API Javascript 扩展模式

javascript - 当未设置显式值且字段包含任何字符时,Angular ng-maxlength 将字段标记为无效

javascript - 使用 express bodyParser 后 request.body undefined

javascript - 验证 html-javascript

javascript - 如何以面向对象的方式创建 DOM 元素?

c# - asp.net treeview 复选框选择

javascript - $.Callbacks Angular 替代方案或管理许多回调的方法

javascript - Angular Change Detection Promises - 混合应用

node.js - 我应该使用 npm install 还是 npm update 来使我的代码保持最新?

javascript - 如何使用 XAMPP 或 node.js 让 AJAX 在本地服务器上工作