我正在构建一个web API。我发现每当我使用Chrome POST, GET到我的API,总是有一个选项请求发送之前的真正的请求,这是相当恼人的。目前,我让服务器忽略任何OPTIONS请求。现在我的问题是,发送一个OPTIONS请求来增加服务器的负载有什么好处呢?有没有办法完全停止浏览器发送OPTIONS请求?
当前回答
编辑2018-09-13:在这个响应的末尾增加了关于这个飞行前请求以及如何避免它的一些精度。
OPTIONS请求是我们在跨源资源共享(CORS)中所谓的飞行前请求。
当您在特定情况下跨不同来源提出请求时,它们是必要的。
某些浏览器会发出这种预运行请求,作为一种安全措施,以确保正在执行的请求是受服务器信任的。 这意味着服务器理解在请求上发送的方法、源和头是安全的。
当您试图执行跨源请求时,您的服务器不应该忽略这些请求,而是应该处理这些请求。
好的资源可以在这里找到http://enable-cors.org/
处理这些问题的一种方法是确保对于任何具有OPTIONS方法的路径,服务器都会发送带有此报头的响应
Access-Control-Allow-Origin: *
这将告诉浏览器服务器愿意回答来自任何来源的请求。
有关如何向服务器添加CORS支持的详细信息,请参阅以下流程图
http://www.html5rocks.com/static/images/cors_server_flowchart.png
编辑2018-09-13
CORS OPTIONS请求仅在某些情况下被触发,如MDN文档中所解释的:
Some requests don’t trigger a CORS preflight. Those are called “simple requests” in this article, though the Fetch spec (which defines CORS) doesn’t use that term. A request that doesn’t trigger a CORS preflight—a so-called “simple request”—is one that meets all the following conditions: The only allowed methods are: GET HEAD POST Apart from the headers set automatically by the user agent (for example, Connection, User-Agent, or any of the other headers with names defined in the Fetch spec as a “forbidden header name”), the only headers which are allowed to be manually set are those which the Fetch spec defines as being a “CORS-safelisted request-header”, which are: Accept Accept-Language Content-Language Content-Type (but note the additional requirements below) DPR Downlink Save-Data Viewport-Width Width The only allowed values for the Content-Type header are: application/x-www-form-urlencoded multipart/form-data text/plain No event listeners are registered on any XMLHttpRequestUpload object used in the request; these are accessed using the XMLHttpRequest.upload property. No ReadableStream object is used in the request.
其他回答
我已经解决了这个问题。
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS' && ENV == 'devel') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header("HTTP/1.1 200 OK");
die();
}
这只是为了发展。这样我就可以等待9毫秒和500毫秒,而不是8秒和500毫秒。我可以这样做,因为生产JS应用程序将在同一台机器上生产,所以没有选项,但开发是我的本地。
编辑2018-09-13:在这个响应的末尾增加了关于这个飞行前请求以及如何避免它的一些精度。
OPTIONS请求是我们在跨源资源共享(CORS)中所谓的飞行前请求。
当您在特定情况下跨不同来源提出请求时,它们是必要的。
某些浏览器会发出这种预运行请求,作为一种安全措施,以确保正在执行的请求是受服务器信任的。 这意味着服务器理解在请求上发送的方法、源和头是安全的。
当您试图执行跨源请求时,您的服务器不应该忽略这些请求,而是应该处理这些请求。
好的资源可以在这里找到http://enable-cors.org/
处理这些问题的一种方法是确保对于任何具有OPTIONS方法的路径,服务器都会发送带有此报头的响应
Access-Control-Allow-Origin: *
这将告诉浏览器服务器愿意回答来自任何来源的请求。
有关如何向服务器添加CORS支持的详细信息,请参阅以下流程图
http://www.html5rocks.com/static/images/cors_server_flowchart.png
编辑2018-09-13
CORS OPTIONS请求仅在某些情况下被触发,如MDN文档中所解释的:
Some requests don’t trigger a CORS preflight. Those are called “simple requests” in this article, though the Fetch spec (which defines CORS) doesn’t use that term. A request that doesn’t trigger a CORS preflight—a so-called “simple request”—is one that meets all the following conditions: The only allowed methods are: GET HEAD POST Apart from the headers set automatically by the user agent (for example, Connection, User-Agent, or any of the other headers with names defined in the Fetch spec as a “forbidden header name”), the only headers which are allowed to be manually set are those which the Fetch spec defines as being a “CORS-safelisted request-header”, which are: Accept Accept-Language Content-Language Content-Type (but note the additional requirements below) DPR Downlink Save-Data Viewport-Width Width The only allowed values for the Content-Type header are: application/x-www-form-urlencoded multipart/form-data text/plain No event listeners are registered on any XMLHttpRequestUpload object used in the request; these are accessed using the XMLHttpRequest.upload property. No ReadableStream object is used in the request.
不能,但可以使用JSONP避免CORS。
在使用代理拦截请求并写入适当的标头的情况下,可以解决这个问题。 在Varnish的特殊情况下,这些将是规则:
if (req.http.host == "CUSTOM_URL" ) {
set resp.http.Access-Control-Allow-Origin = "*";
if (req.method == "OPTIONS") {
set resp.http.Access-Control-Max-Age = "1728000";
set resp.http.Access-Control-Allow-Methods = "GET, POST, PUT, DELETE, PATCH, OPTIONS";
set resp.http.Access-Control-Allow-Headers = "Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since";
set resp.http.Content-Length = "0";
set resp.http.Content-Type = "text/plain charset=UTF-8";
set resp.status = 204;
}
}
在花了一整天半的时间试图解决类似的问题后,我发现这与IIS有关。
我的Web API项目是这样建立的:
// WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
//...
}
我在网上没有CORS特定的配置选项。配置>系统。webServer节点,就像我在很多帖子中看到的那样
全局中没有特定于CORS的代码。Asax或在控制器中作为装饰器
问题在于应用程序池设置。
托管管道模式被设置为经典(更改为集成),标识被设置为网络服务(更改为ApplicationPoolIdentity)
更改这些设置(并刷新应用程序池)为我解决了这个问题。
推荐文章
- 在ASP中设置Access-Control-Allow-OriginNet MVC -最简单的方法
- 什么是HTTP中的“406-不可接受的响应”?
- 最好的轻量级web服务器(只有静态内容)的Windows
- HTTP POST在Java中使用JSON
- 哪些HTTP方法与哪些CRUD方法相匹配?
- 使用HTML形式的PUT方法
- 如何转义哈希字符在URL
- 有没有办法在python中做HTTP PUT
- 跨源请求头(CORS)与PHP头
- 我能在服务器端应用程序(PHP、Ruby、Python等)上读取URL的哈希部分吗?
- 什么是http头“X-XSS-Protection”?
- 在nodejs http中body在哪里。得到回应?
- Ruby:如何将散列转换为HTTP参数?
- 在用nodejs和express创建的REST API中设置响应状态和JSON内容的正确方法
- 为跨源请求设置cookie