close

這個常用到吔~~
by Justin Silverton


While working on an AJAX project over the weekend, I ran into the following issue: (through a GET request), every time I tried to call a certain function, It was returning the same data (which was supposed to be different each time)


I first tried the following (which should disable browser caching):


(in PHP)


header( “Expires: Mon, 26 Jul 1997 05:00:00 GMT” );
header( “Last-Modified: ” . gmdate( “D, d M Y H:i:s” ) . ” GMT” );
header( “Cache-Control: no-cache, must-revalidate” );
header( “Pragma: no-cache” );


The data still did not change.


I finally came to the following solutions:


1) use a POST request. When using with xmlhttprequest, it is slightly more complicated.


2) add a unique identifier to the end of my GET url.


I choose #2. A unique Identifier can be created using the current data+time. Here is a simple way to generate this (in Javascript):


var date = new Date();
var timestamp = date.getTime();

createXMLHttpRequest();
xmlHttp.onreadystatechange = handleMessages;
xmlHttp.open(”GET”,”script.php?time=”+timestamp,true);
xmlHttp.send(null);


From http://www.whenpenguinsattack.com/2007/02/05/how-to-stop-ie-from-caching-ajax-requests/


arrow
arrow
    全站熱搜

    hmc0316 發表在 痞客邦 留言(0) 人氣()