duannaiying9662
2015-01-02 17:35
浏览 109
已采纳

重复OPTIONS请求cors / ajax请求

On my site I have an auto-suggest text input that suggests results as the user types. The results are provided by a AJAX calls to an API on a different domain. This means I have to use CORS to allow the requests.

It is all working quite well, but every time the user types a new character, the browser sends a new OPTIONS request to ensure it is authorized.

See the request sequence here

Is there a way around all these repeated options requests?

My php script receiving the requests has

header("Access-Control-Allow-Origin: http://consent.example.com");

and the requests are all originating from consent.example.com. To be clear, the authorization works just fine, and the request completes successfully, but I don't know why it needs to keep making options calls. It would make sense to me that the browser would cache this.

图片转代码服务由CSDN问答提供 功能建议

在我的网站上,我有一个自动建议的文本输入,用于根据用户类型建议结果。 结果由对不同域上的API的AJAX调用提供。 这意味着我必须使用CORS来允许请求。

这一切都运行良好,但每次用户键入新字符时,浏览器都会发送一个新的OPTIONS请求以确保它 授权。

\ n

是否有解决所有这些重复选项请求的方法?

我的php脚本接收请求

  header(“  Access-Control-Allow-Origin:http://consent.example.com“); 
   
 
 

请求全部来自 consent.example .COM 。 要明确的是,授权工作正常,请求成功完成,但我不知道为什么需要继续进行选项调用。 我觉得浏览器会缓存它。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dougou7782 2015-01-02 17:53
    最佳回答

    According to RFC 2616 ("Hypertext Transfer Protocol -- HTTP/1.1"), section 9.2:

    9.2 OPTIONS

    ...

    Responses to this method are not cacheable.

    The HTTP spec explicitly disallows caching OPTIONS responses.

    It is worth noting that the GET responses do not employ caching either (I see that customers?search=alex is 200 each time). This is simply because the server chooses not to send 304 responses for that request, or your browser doesn't let the server know it has a cached copy, by an If-Modified-Since or If-None-Match request header.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题