I have built a test app on android using this cool-ass PhoneGap framework, but the problem is the AJAX request can get data from a remote server using jQuery AJAX but once it completes one req开发者_运维问答uest the data seems to stick and when I change the response coming from the server, the change is not reflected upon a new request. Here is the AJAX Request in the application itself:
$.getJSON('http://example.com/test1.php', function(data){
alert(data.rec);
});
Here is the PHP code on the remote server:
header('Content-type: application/json');
$arr = array("resp"=>"response has changed");
echo json_encode($arr);
My question is, why? Why won't the change reflect itself from the application?
In order for jQuery AJAX to not cache the server response on shorthand AJAX requests like $.getJSON()
, you have to setup a global setting that tells subsequent AJAX requests to not cache the server response. You can do this using $.ajaxSetup()
. Do it like this
$.ajaxSetup({
cache: false
});
In order to further prevent caching of server responses use the headers that Tincho Revert posted in his response, they are as follows:
header("Expires: Tue, 01 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
Maybe WebKit (the HTML5 browser running your Phonegap application) is caching the server response. To avoid that behavior please add this at the beggining of your web service/PHP script:
header("Expires: Tue, 01 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
精彩评论