如何检查是否jQuery.ajax()请求头状态为" 304未修改"? [英] How to check if jQuery.ajax() request header Status is "304 Not Modified"?

查看:220
本文介绍了如何检查是否jQuery.ajax()请求头状态为" 304未修改"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查是否 jQuery.ajax()请求头状态为304未修改?

How to check if jQuery.ajax() request header Status is "304 Not Modified"?

jqXHR.status 通常返回 200 ,即使请求头是304未修改。

jqXHR.status usually returns 200, even when requested header is "304 Not Modified".

ifModified:真正的 <一个href="http://forum.jquery.com/topic/how-to-fix-browser-cache-and-notmodified-respond-for-json-jquery-ajax-ifmodified-true-break-on-data-respond">does不由很多,因为它打破了XHR数据请求。

ifModified:true does not help a lot because it breaks XHR data request.

推荐答案

而无需手动处理缓存头,这是不可能的。通常情况下,304响应不通过 XHR API 的提供:

Without handling cache headers manually, it is not possible. Normally, 304 responses are not made available through the XHR API:

有关304未修改反应,是一个用户代理而产生的条件请求的用户代理必须充当如果服务器给出了与相应的内容200 OK响应

For 304 Not Modified responses that are a result of a user agent generated conditional request the user agent must act as if the server gave a 200 OK response with the appropriate content.

jQuery的通常不知道有一个304的响应,因为浏览器会告诉礼貌谎言的JavaScript所实际发生的事情在网络上。

jQuery normally doesn't know there was a 304 response, because the browser tells polite lies to JavaScript about what is actually happening over the network.

不过也有好消息(种):你的可以的获得Ajax来产生一个304响应,但只能通过手动设置的 HTTP缓存头 如果修改 - 因为如果 - 无 - 匹配请求:

But there is good news (kind of): you can get Ajax to produce a 304 response, but only by manually setting the HTTP cache headers If-Modified-Since or If-None-Match in the request:

用户代理必须允许 setRequestHeader()通过设置请求头(例如,如果 - 无 - 匹配如果修改 - 因为),在这种情况下,304未修改响应必经之地。

The user agent must allow setRequestHeader() to override automatic cache validation by setting request headers (e.g., If-None-Match, If-Modified-Since), in which case 304 Not Modified responses must be passed through.

所以,你可以用code这样的:

So, you can use code like:

var xhr = new XMLHttpRequest();
xhr.open("GET", "foo.html");
xhr.setRequestHeader("If-Modified-Since", "Fri, 15 Feb 2013 13:43:19 GMT");
xhr.send();

一个主要困难是的你怎么知道最后修改日期或 ETag的来送?浏览器有它用来发送请求缓存信息,但不会将这些信息与JavaScript的。幸运的是,jQuery的跟踪的的Last-Modified 的ETag 从Ajax响应报头,这样你就可以使用 ifModified:真正的有jQuery的设置这些标题就发送对资源的请求下一次值

One fundamental difficulty is how do you know what last-modified date or ETag to send? The browser has cache information that it uses for sending requests, but it won't share that information with JavaScript. Fortunately, jQuery keeps track of the Last-Modified and ETag headers from Ajax responses, so you can use ifModified:true to have jQuery set those header values the next time it sends a request for that resource.

有两点需要注意一下:

  • 304的响应不携带数据。这是由设计。假设是,如果你已经选择使用缓存,您的应该有数据的副本已经在你的缓存的!如果从断绝没有得到数据是一个问题(如,因为你不已经有数据)你为什么要使用缓存?高速缓存时,应使用你手头上的旧数据,只希望新数据;由此,以304取回没有数据不应该是一个问题。

  • 304 responses do not carry data. This is by design. The assumption is that if you have elected to use caching, you should have a copy of the data already in your cache! If getting no data from the sever is a problem (i.e., because you don't already have that data) why are you using caching? Caching should be used when you have the old data on hand and only want new data; thus, getting back no data with a 304 should not be a problem.

jQuery的必须有一个最后修改日期或一个ETag(与来使用。如果无 - 匹配)从previous请求存储。这个过程是这样的:

jQuery must have a last-modified date or an ETag (to use with If-None-Match) stored from a previous request. The process goes like this:

  • 第一次提取:jQuery有没有缓存信息,因此它不会发送如果修改 - 因为如果-无 - 比赛。当响应返回时,服务器可能会宣布最后修改数据或ETag的,它jQuery的商店以供将来使用。

  • First fetch: jQuery has no cache information, so it doesn't send If-Modified-Since or If-None-Match. When the response comes back, the server may announce a last-modified data or an ETag, which jQuery stores for future use.

后续回迁:jQuery有从最后高速缓存信息读取和转发该数据到服务器。如果资源没有改变,Ajax请求得到一个304响应。如果资源发生了变化,Ajax请求得到一个200响应,以及​​新的缓存信息的jQuery以用于下一次取。

Subsequent fetches: jQuery has cache information from the last fetch and forwards that data to the server. If the resource has not changed, the Ajax request gets a 304 response. If the resource has changed, the Ajax request gets a 200 response, along with new cache information for jQuery to use for its next fetch.

的jQuery的的但是仍然存在重新加载页面的缓存信息(例如,在饼干)。因此,在重新加载页面的第一次提取的资源会的永远的是304,因为jQuery没有缓存信息发送(即,我们重新回到了先取的情况下)。没有理由的jQuery的无法的持续高速缓存信息,但present事实并非如此。

jQuery does not persist cache information (e.g., in cookies) between page reloads, however. Therefore, the first fetch of a resource after a page reload will never be a 304, because jQuery has no cache information to send (i.e., we reset back to the "first fetch" case). There is no reason why jQuery couldn't persist cache information, but at present it doesn't.

这里的底线是,你可以使用缓存头获得的JavaScript 304响应,但不能访问的浏览器自身的的ETag或最后修改日期为一个特定的资源。因此,浏览器本身可能知道缓存关于资源的信息,但你的JavaScript code没有。在这种情况下,浏览器将使用缓存标头可能得到真正的304响应,但转发200响应你的JavaScript code,因为JavaScript没有派任何缓存信息。

The bottom line here is that you can use cache headers to get a JavaScript 304 response, but you can't access the browser's own ETag or last-modified date for a particular resource. So, the browser itself might know caching information about a resource, but your JavaScript code does not. In that case, the browser will use its cache headers to potentially get a real 304 response, but forward a 200 response to your JavaScript code, because JavaScript didn't send any cache information.

这是不可能使JavaScript的304的要求完全一致与实际网络304的响应,因为您的浏览器,通过你的JavaScript code称为高速缓存信息已知的高速缓存信息可能会有所不同在联合国predictable方式。但是,让304请求正确的大部分时间是最实际的发展需要足够好了。

It is not possible to make JavaScript 304 requests align perfectly with actual network 304 responses, because the cache information known by your browser and the cache information known by your JavaScript code may differ in unpredictable ways. However, getting 304 requests correctly most of the time is good enough for most practical development needs.

下面是写在Node.js的一个简单的服务器示例(但它应该是足够简单地移植到其他汉语语言):

Here's a brief server example written in Node.js (but it should be simple enough to port to other langauges):

require("http").createServer(function (req, res) {
  console.log(req.headers["if-modified-since"]);

  // always send Last-Modifed header
  var lastModDate = "Fri, 13 Feb 2013 13:43:19 GMT";
  res.setHeader("Last-Modified", lastModDate);

  // if the request has a If-Modified-Since header,
  //   and it's newer than the last modification,
  //   then send a 304 response; otherwise send 200
  if(req.headers["if-modified-since"] &&
     Date.parse(lastModDate) <= Date.parse(req.headers["if-modified-since"])) {
    console.log("304 -- browser has it cached");
    res.writeHead(304, {'Content-Type': 'text/plain'});
    res.end();
  } else {
    console.log("200 -- browser needs it fresh");
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('some content');
  }

}).listen(8080);

在运行该服务器,您可以加载页面在浏览器和浏览器的控制台执行两个不同的测试:

When running this server, you can load the page in your browser and perform two different tests in your browser console:

var xhr = new XMLHttpRequest();
xhr.open("GET", "/");
xhr.send();
xhr.onload = function() { console.log(xhr.status); }

该脚本将总能看到 200 的响应,即使浏览器提供了一个如果修改 - 因为请求头并得到 304 (将发生后的第一个所有的请求,浏览器看到后,服务器的最后一改性的响应头)。

This script will always see a 200 response, even if the browser supplies a If-Modified-Since request header and gets a 304 (which will happen all requests after the first, after the browser sees the server's Last-Modifed response header).

相反,该脚本将的总是的看到304响应:

By contrast, this script will always see 304 response:

var xhr = new XMLHttpRequest();
xhr.open("GET", "/");
xhr.setRequestHeader("If-Modified-Since", "Fri, 15 Feb 2013 13:43:19 GMT");
xhr.send();
xhr.onload = function() { console.log(xhr.status); }

脚本提供自己的如果修改 - 因为请求头(服务器的最后修改日期后2天);它不依赖于任何浏览器用品如果修改 - 因为,并因此被允许(每XHR规范),看看304响应。

The script supplies its own If-Modified-Since request header (two days after the server's last-modified date); it does not rely on whatever the browser supplies for If-Modified-Since, and therefore is allowed (per the XHR spec) to see 304 responses.

最后,该脚本将总能看到 200

Finally, this script will always see a 200:

var xhr = new XMLHttpRequest();
xhr.open("GET", "/");
xhr.setRequestHeader("If-Modified-Since", "Fri, 12 Feb 2013 13:43:19 GMT");
xhr.send();
xhr.onload = function() { console.log(xhr.status); }

这是因为该脚本使用如果修改 - 因为这是之前服务器的最后修改日期,所以,服务器始终发送 200 。该服务器将不发送 304 ,因为它假定客户端不具有的最新版本缓存副本(即客户端宣布,它自二月看到的变化12,但对02月13日的变化,客户端显然还没有看到)。

This is because the script uses a If-Modified-Since that is prior to the server's last-modified date, so the server always sends a 200. The server won't send a 304 because it assumes the client doesn't have a cached copy of the most recent version (i.e., the client announces that it's seen changes since Feb 12, but there was a change on Feb 13 that the client apparently hasn't seen).

这篇关于如何检查是否jQuery.ajax()请求头状态为&QUOT; 304未修改&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆