为什么发送OPTIONS请求,我可以禁用它吗? [英] Why is an OPTIONS request sent and can I disable it?

查看:2438
本文介绍了为什么发送OPTIONS请求,我可以禁用它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个web api。我发现只要我使用Chrome进行POST,GET到我的API,就会在真实请求之前发送一个OPTION请求,这非常烦人。目前我让服务器忽略任何OPTIONS请求。现在我的问题是发送OPTION请求以加倍服务器负载是什么好事?有没有办法完全阻止浏览器发送OPTIONS请求?

I am building a web api. I found whenever I use Chrome to POST, GET to my API, there is always an OPTION request sent before the real request, which is quite annoying. Currently I get the server to ignore any OPTIONS request. Now my questions is what's good to send an OPTION request to double the server's load? Is there any way to completely stop the browser from sending OPTIONS requests?

推荐答案

OPTIONS 请求就是我们所说的飞行前请求跨域资源共享(CORS)

OPTIONS requests are what we call pre-flight requests in Cross-origin resource sharing (CORS).

他们当您在特定情况下跨不同来源提出请求时,这是必要的。

They are necessary when you're making requests across different origins in specific situations.

这个飞行前请求由一些浏览器作为安全措施,以确保服务器信任所完成的请求。
意味着服务器理解在请求上发送的方法,来源和标头可以安全地执行操作。

This pre-flight request is made by some browsers as a safety measure to ensure that the request being done is trusted by the server. Meaning the server understands that the method, origin and headers being sent on the request are safe to act upon.

当您尝试进行跨源请求时,您的服务器不应忽略但处理这些请求。

Your server should not ignore but handle these requests whenever you're attempting to do cross origin requests.

一个很好的资源可以在这里找到 http://enable-cors.org/

A good resource can be found here http://enable-cors.org/

处理这些变得舒服的一种方法是确保对于具有 OPTIONS 方法的任何路径,服务器发送带有此标头的响应

A way to handle these to get comfortable is to ensure that for any path with OPTIONS method the server sends a response with this header

Access-Control-Allow-Origin:*

这将告诉浏览器服务器愿意回答来自任何来源的请求。

This will tell the browser that the server is willing to answer requests from any origin.

有关如何向服务器添加CORS支持的更多信息,请参阅以下流程图

For more information on how to add CORS support to your server see the following flowchart

http://www.html5rocks.com/static/images/cors_server_flowchart.png

这篇关于为什么发送OPTIONS请求,我可以禁用它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆