Adding support for etags in $.ajax() - and simplified the if-modified-since implementation. Thanks to Lawrence for the patch! Closes ticket #4764.

This commit is contained in:
John Resig 2009-06-15 13:36:12 +00:00
parent 030ae67715
commit 28ab4d3224
4 changed files with 101 additions and 20 deletions

16
test/data/etag.php Normal file
View file

@ -0,0 +1,16 @@
<?php
error_reporting(0);
$ts = $_REQUEST['ts'];
$etag = md5($ts);
$ifNoneMatch = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false;
if ($ifNoneMatch == $etag) {
header('HTTP/1.0 304 Not Modified');
die; // stop processing
}
header("Etag: " . $etag);
echo "OK: " . $etag;
?>

View file

@ -0,0 +1,15 @@
<?php
error_reporting(0);
$ts = $_REQUEST['ts'];
$ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
if ($ifModifiedSince == $ts) {
header('HTTP/1.0 304 Not Modified');
die; // stop processing
}
header("Last-Modified: " . $ts);
echo "OK: " . $ts;
?>

View file

@ -874,6 +874,58 @@ test("data option: evaluate function values (#2806)", function() {
})
});
test("jQuery.ajax - If-Modified-Since support", function() {
expect( 3 );
stop();
var url = "data/if_modified_since.php?ts=" + new Date();
jQuery.ajax({
url: url,
ifModified: true,
success: function(data, status) {
equals(status, "success");
jQuery.ajax({
url: url,
ifModified: true,
success: function(data, status) {
equals(status, "notmodified");
ok(data == null, "response body should be empty")
start();
}
});
}
});
});
test("jQuery.ajax - Etag support", function() {
expect( 3 );
stop();
var url = "data/etag.php?ts=" + new Date();
jQuery.ajax({
url: url,
ifModified: true,
success: function(data, status) {
equals(status, "success");
jQuery.ajax({
url: url,
ifModified: true,
success: function(data, status) {
equals(status, "notmodified");
ok(data == null, "response body should be empty")
start();
}
});
}
});
});
}
//}