pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何檢查 jQuery.ajax() 請求標(biāo)頭狀態(tài)是否為“304 未

How to check if jQuery.ajax() request header Status is quot;304 Not Modifiedquot;?(如何檢查 jQuery.ajax() 請求標(biāo)頭狀態(tài)是否為“304 未修改?)
本文介紹了如何檢查 jQuery.ajax() 請求標(biāo)頭狀態(tài)是否為“304 未修改"?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

如何檢查 jQuery.ajax() 請求頭狀態(tài)是否為304 Not Modified"?

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

jqXHR.status 通常返回 200,即使請求的標(biāo)頭是304 Not Modified".

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

ifModified:true 沒有太大幫助,因為它破壞了 XHR 數(shù)據(jù)請求.

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

推薦答案

不手動處理緩存頭是不可能的.通常,無法通過 XHR API 獲得 304 響應(yīng):

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

對于作為用戶代理生成的條件請求的結(jié)果的 304 Not Modified 響應(yīng)用戶代理必須像服務(wù)器給出具有適當(dāng)內(nèi)容的 200 OK 響應(yīng)一樣行事.

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 響應(yīng),因為瀏覽器會禮貌地向 JavaScript 撒謊,告訴 JavaScript 網(wǎng)絡(luò)上實際發(fā)生的事情.

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 產(chǎn)生 304 響應(yīng),但只能通過手動設(shè)置 HTTP 緩存頭 If-Modified-SinceIf-None-Match 在請求中:

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() 通過設(shè)置請求標(biāo)頭來覆蓋自動緩存驗證(例如,If-None-Match、If-Modified-自),在這種情況下必須通過 304 Not Modified 響應(yīng).

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.

所以,你可以使用如下代碼:

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();

一個基本的困難是你怎么知道要發(fā)送的最后修改日期或ETag?em> 瀏覽器具有用于發(fā)送請求的緩存信息,但它不會與 JavaScript 共享該信息.幸運的是,jQuery 會跟蹤來自 Ajax 響應(yīng)的 Last-ModifiedETag 標(biāo)頭,因此您可以使用 ifModified:true 讓 jQuery 設(shè)置這些標(biāo)頭下次發(fā)送對該資源的請求時的標(biāo)頭值.

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 響應(yīng)不攜帶數(shù)據(jù).這是設(shè)計使然.假設(shè)是,如果您選擇使用緩存,您應(yīng)該已經(jīng)在緩存中擁有數(shù)據(jù)的副本!如果沒有從服務(wù)器獲取數(shù)據(jù)是一個問題(即,因為您還沒有該數(shù)據(jù))為什么要使用緩存?當(dāng)您手頭有舊數(shù)據(jù)并且只需要新數(shù)據(jù)時,應(yīng)該使用緩存;因此,使用 304 取回任何數(shù)據(jù)應(yīng)該不是問題.

  • 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(與 If-None-Match 一起使用).流程是這樣的:

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 沒有緩存信息,所以它不會發(fā)送 If-Modified-SinceIf-None-Match.當(dāng)響應(yīng)返回時,服務(wù)器可能會宣布最后修改的數(shù)據(jù)或 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.

后續(xù)提取:jQuery 擁有上次提取的緩存信息并將該數(shù)據(jù)轉(zhuǎn)發(fā)到服務(wù)器.如果資源沒有改變,Ajax 請求會得到 304 響應(yīng).如果資源已更改,Ajax 請求會收到 200 響應(yīng),以及新的緩存信息供 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 在頁面重新加載之間保留緩存信息(例如,在 cookie 中).因此,頁面重新加載后第一次獲取資源永遠不會是 304,因為 jQuery 沒有要發(fā)送的緩存信息(即,我們重置回第一次獲取"情況).jQuery 沒有理由不能持久化緩存信息,但目前它沒有.

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.

這里的底線是您可以使用緩存標(biāo)頭來獲取 JavaScript 304 響應(yīng),但您不能訪問 瀏覽器自己的 ETag 或特定資源的上次修改日期.因此,瀏覽器本身可能知道有關(guān)資源的緩存信息,但您的 JavaScript 代碼不知道.在這種情況下,瀏覽器將使用其緩存標(biāo)頭來獲得真正的 304 響應(yīng),但會將 200 響應(yīng)轉(zhuǎn)發(fā)到您的 JavaScript 代碼,因為 JavaScript 沒有發(fā)送任何緩存信息.

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 請求與實際的網(wǎng)絡(luò) 304 響應(yīng)完全一致,因為您的瀏覽器知道的緩存信息和您的 JavaScript 代碼知道的緩存信息可能以不可預(yù)知的方式不同.但是,大多數(shù)情況下正確獲取 304 請求足以滿足大多數(shù)實際開發(fā)需求.

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 編寫的簡短服務(wù)器示例(但它應(yīng)該足夠簡單,可以移植到其他語言):

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);

運行此服務(wù)器時,您可以在瀏覽器中加載頁面并在瀏覽器控制臺中執(zhí)行兩種不同的測試:

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 響應(yīng),即使瀏覽器提供 If-Modified-Since 請求標(biāo)頭并獲得 304(這將發(fā)生在第一個請求之后,在瀏覽器看到服務(wù)器的 Last-Modifed 響應(yīng)頭之后).

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 響應(yīng):

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); }

腳本提供自己的 If-Modified-Since 請求標(biāo)頭(服務(wù)器最后修改日期后兩天);它不依賴于瀏覽器為 If-Modified-Since 提供的任何內(nèi)容,因此允許(根據(jù) XHR 規(guī)范)查看 304 響應(yīng).

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); }

這是因為腳本使用了早于服務(wù)器最后修改日期的 If-Modified-Since,所以服務(wù)器總是發(fā)送 200.服務(wù)器不會發(fā)送 304 因為它假定客戶端沒有最新版本的緩存副本(即,客戶端宣布自 2 月 12 日以來它看到了更改,但是有一個2 月 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).

這篇關(guān)于如何檢查 jQuery.ajax() 請求標(biāo)頭狀態(tài)是否為“304 未修改"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創(chuàng)建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設(shè)置值/標(biāo)簽的問題)
how to unit-test private methods in jquery plugins?(如何對 jquery 插件中的私有方法進行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網(wǎng)站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當(dāng)文本框獲得焦點時選擇所有內(nèi)容)
主站蜘蛛池模板: PSI渗透压仪,TPS酸度计,美国CHAI PCR仪,渗透压仪厂家_价格,微生物快速检测仪-华泰和合(北京)商贸有限公司 | 无尘烘箱_洁净烤箱_真空无氧烤箱_半导体烤箱_电子防潮柜-深圳市怡和兴机电 | 机械立体车库租赁_立体停车设备出租_智能停车场厂家_春华起重 | 智能化的检漏仪_气密性测试仪_流量测试仪_流阻阻力测试仪_呼吸管快速检漏仪_连接器防水测试仪_车载镜头测试仪_奥图自动化科技 | 金属清洗剂,防锈油,切削液,磨削液-青岛朗力防锈材料有限公司 | 南京蜂窝纸箱_南京木托盘_南京纸托盘-南京博恒包装有限公司 | 机械立体车库租赁_立体停车设备出租_智能停车场厂家_春华起重 | 一航网络-软件测评官网 | 佛山商标注册_商标注册代理|专利注册申请_商标注册公司_鸿邦知识产权 | 旋转/数显粘度计-运动粘度测定仪-上海平轩科学仪器 | 银川美容培训-美睫美甲培训-彩妆纹绣培训-新娘化妆-学化妆-宁夏倍莱妮职业技能培训学校有限公司 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 超声波分散机-均质机-萃取仪-超声波涂料分散设备-杭州精浩 | 超声波气象站_防爆气象站_空气质量监测站_负氧离子检测仪-风途物联网 | 砂尘试验箱_淋雨试验房_冰水冲击试验箱_IPX9K淋雨试验箱_广州岳信试验设备有限公司 | 干粉砂浆设备-干粉砂浆生产线-干混-石膏-保温砂浆设备生产线-腻子粉设备厂家-国恒机械 | 渣油泵,KCB齿轮泵,不锈钢齿轮泵,重油泵,煤焦油泵,泊头市泰邦泵阀制造有限公司 | 黄石东方妇产医院_黄石妇科医院哪家好_黄石无痛人流医院 | 塑料托盘厂家直销-吹塑托盘生产厂家-力库塑业【官网】 | 斗式提升机,斗式提升机厂家-淄博宏建机械有限公司 | 玉米加工设备,玉米深加工机械,玉米糁加工设备.玉米脱皮制糁机 华豫万通粮机 | 浩方智通 - 防关联浏览器 - 跨境电商浏览器 - 云雀浏览器 | 台湾HIWIN上银直线模组|导轨滑块|TBI滚珠丝杆丝杠-深圳汉工 | 跨境物流_美国卡派_中大件运输_尾程派送_海外仓一件代发 - 广州环至美供应链平台 | 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | 磁力轮,磁力联轴器,磁齿轮,钕铁硼磁铁-北京磁运达厂家 | 电磁流量计_智能防腐防爆管道式计量表-金湖凯铭仪表有限公司 | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 聚丙烯酰胺_阴离子_阳离子「用量少」巩义亿腾厂家直销,售后无忧 聚合甘油__盐城市飞龙油脂有限公司 | 北京翻译公司_同传翻译_字幕翻译_合同翻译_英语陪同翻译_影视翻译_翻译盖章-译铭信息 | 电磁铁_推拉电磁铁_机械手电磁吸盘电磁铁厂家-广州思德隆电子公司 | 远程会诊系统-手术示教系统【林之硕】医院远程医疗平台 | 橡胶膜片,夹布膜片,橡胶隔膜密封,泵阀设备密封膜片-衡水汉丰橡塑科技公司网站 | 安徽免检低氮锅炉_合肥燃油锅炉_安徽蒸汽发生器_合肥燃气锅炉-合肥扬诺锅炉有限公司 | 温泉机设备|温泉小镇规划设计|碳酸泉设备 - 大连连邦温泉科技 | 高空重型升降平台_高空液压举升平台_高空作业平台_移动式升降机-河南华鹰机械设备有限公司 | 全自动在线分板机_铣刀式在线分板机_曲线分板机_PCB分板机-东莞市亿协自动化设备有限公司 | 818手游网_提供当下热门APP手游_最新手机游戏下载 | 北京发电机出租_发电机租赁_北京发电机维修 - 河北腾伦发电机出租 | 河南膏药贴牌-膏药代加工-膏药oem厂家-洛阳今世康医药科技有限公司 | 陶瓷砂磨机,盘式砂磨机,棒销式砂磨机-无锡市少宏粉体科技有限公司 | 成都热收缩包装机_袖口式膜包机_高速塑封机价格_全自动封切机器_大型套膜机厂家 |