时间使用jQuery ajax的两个CORS请求之间的时间间隔 [英] Time Interval between two CORS requests using jquery ajax
问题描述
我正在使用jQuery $。ajax
向Web服务发出CORS请求。按照标准,有一个预先请求的请求,然后是实际的POST请求。
我注意到,每次尝试创建Web服务时都有两个请求仅在两次请求之间有一段时间间隔。
如果我连续不断地进行Web服务调用时间差距(如两个请求之间少于1秒),则飞行前丢失。
每次我如何避免这个预先请求的请求?
这个时间间隔是什么时间?
这是Chrome浏览器特有的东西吗?
浏览器在实现飞行前缓存方面的差异。不幸的是,W3C规范本身并不能解释您在飞行前高速缓存中观察到的细微差别。对于阅读这个问题的其他人,我想解释OP何时说
前请求,他指的是OPTIONS
请求位于跨源POST
请求之前。OPTIONS
请求用于查询API并确定哪些HTTP方法可用于跨源请求。通常,您会希望看到对OPTIONS
请求的这种响应: c $ c> Access-Control-Allow-Origin:*
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type,X-Requested-With
Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE,PUT
使用谷歌浏览器,我会向您推荐 Webkit 源代码的适用部分:
默认的飞行前缓存超时时间为5秒:
static const auto defaultPreflightCacheTimeout = std :: chrono :: seconds(5);
最大值为5分钟:
static const auto maxPreflightCacheTimeout = std :: chrono :: seconds(600);
服务器可以使用 <$ c $为执行前请求指定超时值c> Access-Control-Max-Age 字段,但是 Webkit浏览器强制执行最大超时为5分钟。
回答您的问题:
我怎样才能避免每次这个预先飞行要求?您需要将
Access-Control-Max-Age
设置为600 在您的API响应标头中对OPTIONS
请求。 $ b
这是什么时间间隔?
对于Webkit浏览器(即谷歌浏览器),默认的超时值是 5秒即可。这就是为什么你会在每次POST请求之前看到预先请求的请求,但是如果你迅速提交POST请求,那么你就不会看到额外的预先请求。
这是Chrome浏览器的特定功能吗?
是,存在差异浏览器之间如何实现飞行前缓存。 W3C规范并未详细说明在Web浏览器中构建飞行前高速缓存功能所需的一切。
I am making CORS request to a web service using jQuery
$.ajax
. As per standard there is a pre-flight request and then the actual POST request.What I have noticed is, there are two requests everytime I try to make a web service call (one pre-flight and one actual POST request) only If there is a time interval between the two requests.
If I keep making the web service call successively without any time gap (like less than 1 second between two requests) then the pre-flight is missing.
How can I avoid this pre-flight request every time?
What is this time interval?
Is this something specific to Chrome browser?
解决方案There are differences between browsers in how they implement pre-flight caching. Unfortunately the W3C spec alone does not explain the nuances you've observed in pre-flight caching.
For others reading this question, I'd like to explain when the OP says pre-flight request he is referring to the
OPTIONS
request that precedes a cross-originPOST
request. TheOPTIONS
request is used to query an API and determine which HTTP methods are permissible for cross-origin requests. Typically, you'd expect to see this type of response to anOPTIONS
request:Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: Content-Type, X-Requested-With Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
Since you are working with Google Chrome, I will refer you to the applicable section of the Webkit source code:
The default pre-flight cache timeout is 5 seconds:
static const auto defaultPreflightCacheTimeout = std::chrono::seconds(5);
The maximum value is 5 minutes:
static const auto maxPreflightCacheTimeout = std::chrono::seconds(600);
The server can specify a timeout value for pre-flight requests using the
Access-Control-Max-Age
field in the response header, however the Webkit browser enforces a maximum timeout of 5 minutes.To answer your questions:
How can I avoid this pre flight request every time?
You need to set
Access-Control-Max-Age
to 600 in the header of your API response to theOPTIONS
request.What is this time interval?
For Webkit browsers (i.e., Google Chrome) the default timeout value is 5 seconds. That is why you are seeing the pre-flight request before each POST request, but if you rapidly submit POST requests, then you do not see additional pre-flight requests.
Is this something specific to Chrome browser?
Yes, there are differences between browsers in how pre-flight caching is implemented. The W3C spec does not specify everything needed to build out pre-flight caching functionality in a web browser.
这篇关于时间使用jQuery ajax的两个CORS请求之间的时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!