🌐 AI搜索 & 代理 主页
Skip to content

Commit 28ab4d3

Browse files
committed
Adding support for etags in $.ajax() - and simplified the if-modified-since implementation. Thanks to Lawrence for the patch! Closes ticket #4764.
1 parent 030ae67 commit 28ab4d3

File tree

4 files changed

+101
-20
lines changed

4 files changed

+101
-20
lines changed

src/ajax.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ jQuery.extend({
170170

171171
// Last-Modified header cache for next request
172172
lastModified: {},
173+
etag: {},
173174

174175
ajax: function( s ) {
175176
// Extend the settings, but re-extend 's' so that it can be
@@ -298,10 +299,13 @@ jQuery.extend({
298299
if ( s.data )
299300
xhr.setRequestHeader("Content-Type", s.contentType);
300301

301-
// Set the If-Modified-Since header, if ifModified mode.
302-
if ( s.ifModified )
303-
xhr.setRequestHeader("If-Modified-Since",
304-
jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
302+
// Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
303+
if ( s.ifModified ) {
304+
if (jQuery.lastModified[s.url])
305+
xhr.setRequestHeader("If-Modified-Since", jQuery.lastModified[s.url]);
306+
if (jQuery.etag[s.url])
307+
xhr.setRequestHeader("If-None-Match", jQuery.etag[s.url]);
308+
}
305309

306310
// Set header so the called script knows that it's an XMLHttpRequest
307311
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
@@ -363,16 +367,7 @@ jQuery.extend({
363367
}
364368

365369
// Make sure that the request was successful or notmodified
366-
if ( status == "success" ) {
367-
// Cache Last-Modified header, if ifModified mode.
368-
var modRes;
369-
try {
370-
modRes = xhr.getResponseHeader("Last-Modified");
371-
} catch(e) {} // swallow exception thrown by FF if header is not available
372-
373-
if ( s.ifModified && modRes )
374-
jQuery.lastModified[s.url] = modRes;
375-
370+
if ( status == "success" || status == "notmodified" ) {
376371
// JSONP handles its own success callback
377372
if ( !jsonp )
378373
success();
@@ -467,13 +462,16 @@ jQuery.extend({
467462

468463
// Determines if an XMLHttpRequest returns NotModified
469464
httpNotModified: function( xhr, url ) {
470-
try {
471-
var xhrRes = xhr.getResponseHeader("Last-Modified");
465+
var last_modified = xhr.getResponseHeader("Last-Modified");
466+
var etag = xhr.getResponseHeader("Etag");
472467

473-
// Firefox always returns 200. check Last-Modified date
474-
return xhr.status == 304 || xhrRes == jQuery.lastModified[url];
475-
} catch(e){}
476-
return false;
468+
if (last_modified)
469+
jQuery.lastModified[url] = last_modified;
470+
471+
if (etag)
472+
jQuery.etag[url] = etag;
473+
474+
return xhr.status == 304;
477475
},
478476

479477
httpData: function( xhr, type, s ) {

test/data/etag.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
error_reporting(0);
3+
4+
$ts = $_REQUEST['ts'];
5+
$etag = md5($ts);
6+
7+
$ifNoneMatch = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false;
8+
if ($ifNoneMatch == $etag) {
9+
header('HTTP/1.0 304 Not Modified');
10+
die; // stop processing
11+
}
12+
13+
header("Etag: " . $etag);
14+
echo "OK: " . $etag;
15+
16+
?>

test/data/if_modified_since.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
error_reporting(0);
3+
4+
$ts = $_REQUEST['ts'];
5+
6+
$ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
7+
if ($ifModifiedSince == $ts) {
8+
header('HTTP/1.0 304 Not Modified');
9+
die; // stop processing
10+
}
11+
12+
header("Last-Modified: " . $ts);
13+
echo "OK: " . $ts;
14+
15+
?>

test/unit/ajax.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,58 @@ test("data option: evaluate function values (#2806)", function() {
874874
})
875875
});
876876

877+
test("jQuery.ajax - If-Modified-Since support", function() {
878+
expect( 3 );
879+
880+
stop();
881+
882+
var url = "data/if_modified_since.php?ts=" + new Date();
883+
884+
jQuery.ajax({
885+
url: url,
886+
ifModified: true,
887+
success: function(data, status) {
888+
equals(status, "success");
889+
890+
jQuery.ajax({
891+
url: url,
892+
ifModified: true,
893+
success: function(data, status) {
894+
equals(status, "notmodified");
895+
ok(data == null, "response body should be empty")
896+
start();
897+
}
898+
});
899+
}
900+
});
901+
});
902+
903+
test("jQuery.ajax - Etag support", function() {
904+
expect( 3 );
905+
906+
stop();
907+
908+
var url = "data/etag.php?ts=" + new Date();
909+
910+
jQuery.ajax({
911+
url: url,
912+
ifModified: true,
913+
success: function(data, status) {
914+
equals(status, "success");
915+
916+
jQuery.ajax({
917+
url: url,
918+
ifModified: true,
919+
success: function(data, status) {
920+
equals(status, "notmodified");
921+
ok(data == null, "response body should be empty")
922+
start();
923+
}
924+
});
925+
}
926+
});
927+
});
928+
877929
}
878930

879931
//}

0 commit comments

Comments
 (0)