简体   繁体   English

具有不同数据类型的浏览器缓存请求

[英]Browser caching Requests with different Datatypes

I am trying to get two different resources, that are at the same URI, but have different datatypes. 我正在尝试获取两个不同的资源,它们位于相同的URI,但是具有不同的数据类型。

$.ajax({
  dataType: "application/json",
  url: "http://www.myApp.com/resource",
  success: f
});

$.ajax({
  dataType: "text/html",
  url: "http://www.myApp.com/resource",
  success: f
});

But the second call never leaves the browser. 但是第二个调用永远不会离开浏览器。 The result of the first call is cached and returned to the second call. 第一个调用的结果被缓存并返回到第二个调用。 But it has the wrong datatype! 但是它的数据类型错误! json vs html. json与html。 I do not know whether it is cached by jquery or the browser (chrome)? 我不知道它是由jquery还是由浏览器(chrome)缓存的?

Is there a way to make this work? 有没有办法使这项工作? I need both representations of the resource. 我需要资源的两种表示形式。 I could set the cache to false in the requests, but then I would lose the benefit of caching. 我可以在请求中将缓存设置为false,但是那样我将失去缓存的好处。

Or do I have to define two different resource URIs for the different representations? 还是我必须为不同的表示形式定义两个不同的资源URI?

In dataType, replace "application/json" with "json" and "text/html" with "html" . 在dataType中,将"application/json"替换为"json" ,将"text/html"替换为"html" jQuery recognizes only the following as valid inputs: jQuery仅将以下内容识别为有效输入:

  • xml XML文件
  • json json
  • jsonp jsonp
  • html html
  • text 文本
  • script 脚本

Or a space-separated combination of some of the above. 或上述内容之间用空格分隔的组合。 Any other value will result in a request with an Accept header of */* . 任何其他值将导致请求的Accept标头为*/*

Also, you might want to replace your success callback in favor of $.ajax({...}).done(callback) . 另外,您可能希望替换success $.ajax({...}).done(callback) success回调。 The callback parameter was deprecated in 1.8. 在1.8中不推荐使用callback参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM