I am working on an application that loads HTML content into a DIV tag using the jQuery $.ajax() function. The $.ajax function is run every few seconds to get updates. The HTML content (a PHP script) is rather large and I do not want to load it into the div unless the content has actually changed since the last load. I need to know when a 304 comes back and do nothing - as opposed to loading the HTML into the div from a cache.
I have tried setting:
ifModified: true
and this doesn't seem to return an error as expected.
This leaves me with two questions:
1) Does jQuery think the page is new every time because it's loading a dynamic page - even if the page has not changed since last load?
2) If no to question 1, then is it possible to make a jQuery $.ajax() call and have the error method execute when a 304 is returned?
Here is my test code - just to see if I can catch a 304:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX Conditional GET Request Test</title>
<script type="text/javascript" src="../jquery/jquery-2.1.4.min.js"></script>
</head>
<body>
<input type="submit" name="button" id="button" value="Check for updates" onclick="javascript:runUpdate();" />
<div id="status" style="width: 100px; height: 12px;"></div>
<script type="text/javascript">
function runUpdate()
{
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var time = h+":"+m+":"+s;
$.ajax({
type: "GET",
url: "../dir/phpscriptthatsometimesupdates.php",
ifModified: true,
success: function(data){
$("#status").html("Updated "+time);
},
error: function(data, status, error){
$("#status").html("Not updated "+time);
}
});
}
</script>
</body>
</html>
Every time I run the test it says the file has been updated - even when it has not.
I have also tried using an "If-Modified-Since" using a future date with no success:
headers: {"If-Modified-Since": "Sun, 10 Apr 2016 00:00:00 GMT"}
Server is running:
Apache 2.2.15 (CentOS)
PHP 5.6.17
jQuery 2.1.4
Update:
I tried making a call to a PHP file the used:
header("Last-Modified: Fri, 08 Apr 2016 00:00:00 GMT");
to set the Last-Modified to a time in the past. I checked using my browser tools and saw that the Last-Modified was sent. 5 sequential calls from AJAX to this script did not display an error.
I also read through How to check if jQuery.ajax() request header Status is "304 Not Modified"? but what it describes seems to run contrary to the jQuery documentation explaining the ifModified argument which reads:
ifModified (default: false)
Type: Boolean
Allow the request to be successful only if the response has changed
since the last request. This is done by checking the Last-Modified
header. Default value is false, ignoring the header. In jQuery 1.4 this
technique also checks the 'etag' specified by the server to catch
unmodified data.
or am I missing something? According to the linked post I also tried:
headers: {"If-Modified-Since": "Fri, 08 Apr 2016 00:00:00 GMT"}
but was not successful.