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

重复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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题