<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Couch &#187; AJAX</title>
	<atom:link href="http://www.codecouch.com/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codecouch.com</link>
	<description>The ramblings of two code monkeys</description>
	<lastBuildDate>Thu, 19 Nov 2009 15:47:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to stop Internet Explorer from caching AJAX requests</title>
		<link>http://www.codecouch.com/2009/01/how-to-stop-internet-explorer-from-caching-ajax-requests/</link>
		<comments>http://www.codecouch.com/2009/01/how-to-stop-internet-explorer-from-caching-ajax-requests/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 13:53:34 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[IE7]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=250</guid>
		<description><![CDATA[There are several ways to solve the problem of browsers caching AJAX requests. You can use a 'cache buster' parameter, or you can set the response headers server-side. This article discusses these two options.]]></description>
			<content:encoded><![CDATA[<p>I recently had to revisit the issue of IE&#8217;s default cache settings causing problems when making AJAX requests. Out-of-the-box, the cache setting is &#8216;Automatically&#8217;, where pages are only fetched from the server if the cached content is from an earlier day, old or from an earlier session.</p>
<p>This can be a nightmare to deal with, as you cannot ask all users of a web site to change their browser settings.</p>
<p>One workaround is to use a &#8216;cache buster&#8217; parameter in your GET request. Something like this:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;"><span class="kw2">var</span> urlToFetch <span class="sy0">=</span> <span class="st0">'whatever.php?randNum='</span> <span class="sy0">+</span> <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getTime</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div>

<p>This works very well, but can have drawbacks, depending on your server setup. For example, if your application server sits behind a caching layer, you wouldn&#8217;t want the URL to be different every time for idempotent requests, as the cached version would never get used, and so your application server would get a direct hit every time.</p>
<p>One way around this without using to parameters is to set the &#8216;Cache-Control&#8217; response header to direct the browser how to cache the response.</p>
<p>The &#8216;Cache-Control&#8217; header can have various values, one of which is &#8216;no-cache&#8217;. This tells the browser that it should not cache the response, so the next time the same URL is requested, new content will always be sent (as there is no previous version in the browser&#8217;s cache).</p>
<p>This is how we do it using Groovy:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">response.<span class="me1">addHeader</span> <span class="st0">&quot;Cache-Control&quot;</span><span class="sy0">,</span> <span class="st0">&quot;max-age=0,no-cache,no-store&quot;</span></pre></div></div></div>

<p>So really, which method you use boils down to this: If you know that the cache buster will have a performance hit on your setup, then don&#8217;t use it, otherwise use whichever method you prefer.</p>
<p>Also, as a developer, I can highly recommend changing your IE cache settings to &#8216;Every visit to the page&#8217;.</p>
<div class="seeThrough h2Pod "><div class="seeThroughTop"><span class="tl"></span><span class="tr"></span><span class="t"></span></div><div class="seeThroughMiddle"><span class="r"></span><span class="l"></span><span class="m"></span><div class="seeThroughContent">
	<h2>Update 10/02/2008</h2>
</div></div><div class="seeThroughBottom"><span class="bl"></span><span class="br"></span><span class="b"></span></div></div>

<p>We&#8217;re finding that with some of our automated testing, IE 7 still caches the odd file now and then. To get around this, we have modified our &#8216;Cache-Control&#8217; header to include two non-standard properties, &#8216;post-check&#8217; and &#8216;pre-check&#8217;, and added an &#8216;Expires&#8217; header as well:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="javascript" style="color: #000000;">response.<span class="me1">addHeader</span> <span class="st0">&quot;Cache-Control&quot;</span><span class="sy0">,</span> <span class="st0">&quot;max-age=0,no-cache,no-store,post-check=0,pre-check=0&quot;</span><span class="sy0">;</span>
response.<span class="me1">addHeader</span> <span class="st0">&quot;Expires&quot;</span><span class="sy0">,</span> <span class="st0">&quot;Mon, 26 Jul 1997 05:00:00 GMT&quot;</span></pre></div></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2009/01/how-to-stop-internet-explorer-from-caching-ajax-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using AJAX to update the time on a web page from the server</title>
		<link>http://www.codecouch.com/2008/10/using-ajax-to-update-the-time-on-a-web-page/</link>
		<comments>http://www.codecouch.com/2008/10/using-ajax-to-update-the-time-on-a-web-page/#comments</comments>
		<pubDate>Sun, 12 Oct 2008 11:25:19 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.codecouch.com/?p=130</guid>
		<description><![CDATA[In an attempt to better explain how you can use AJAX to fetch some data from the server and display it on the web page, I have provided a self contained PHP file.]]></description>
			<content:encoded><![CDATA[<p>Sometimes the easiest way to begin to understand something is through an example. In an attempt to better explain how you can use AJAX to fetch some data from the server and then display it on a web page (without refreshing the page), I constructed the following example PHP page:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="php" style="color: #000000;"><span class="kw2">&lt;?</span>
<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">'xhttp'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="re0">$counter</span> <span class="sy0">=</span> <span class="kw3">date</span><span class="br0">&#40;</span><span class="st0">&quot;d M Y h:m:s&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw3">echo</span> <span class="re0">$counter</span><span class="sy0">;</span>
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
<span class="kw2">?&gt;</span>
<span class="sy0">&lt;</span>html<span class="sy0">&gt;</span>
<span class="sy0">&lt;</span>head<span class="sy0">&gt;</span>
<span class="sy0">&lt;</span>title<span class="sy0">&gt;</span>Test some XHTTP<span class="sy0">&lt;/</span>title<span class="sy0">&gt;</span>
<span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
<span class="kw2">function</span> getHTTPObject<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="kw2">var</span> xmlhttp<span class="sy0">;</span>
	<span class="coMULTI">/*@cc_on
	@if (@_jscript_version &gt;= 5)
		try {
			xmlhttp = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/</span>
	<span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>xmlhttp <span class="sy0">&amp;&amp;</span> typeof XMLHttpRequest <span class="sy0">!=</span> <span class="st0">'undefined'</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		try <span class="br0">&#123;</span>
			xmlhttp <span class="sy0">=</span> <span class="kw2">new</span> XMLHttpRequest<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="br0">&#125;</span> catch <span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
			xmlhttp <span class="sy0">=</span> <span class="kw2">false</span><span class="sy0">;</span>
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span>
	<span class="kw1">return</span> xmlhttp<span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
<span class="kw2">function</span> fetchData<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	http <span class="sy0">=</span> getHTTPObject<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
	http<span class="sy0">.</span>open<span class="br0">&#40;</span><span class="st0">'get'</span><span class="sy0">,</span> <span class="st0">&quot;&lt;?= $_SERVER['SCRIPT_NAME']; ?&gt;?xhttp=true&quot;</span><span class="sy0">,</span> <span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">;</span>
	http<span class="sy0">.</span>onreadystatechange <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="kw1">if</span> <span class="br0">&#40;</span>http <span class="sy0">!=</span> <span class="kw2">null</span> <span class="sy0">&amp;&amp;</span> http<span class="sy0">.</span>readyState <span class="sy0">==</span> <span class="nu0">4</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
			document<span class="sy0">.</span>getElementById<span class="br0">&#40;</span><span class="st0">'displayCounter'</span><span class="br0">&#41;</span><span class="sy0">.</span>innerHTML <span class="sy0">=</span> http<span class="sy0">.</span>responseText<span class="sy0">;</span>
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span><span class="sy0">;</span>
	http<span class="sy0">.</span>send<span class="br0">&#40;</span><span class="kw2">null</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
<span class="kw2">function</span> initPage<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	http <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span>
	pageTimerHandle <span class="sy0">=</span> setInterval<span class="br0">&#40;</span><span class="st0">'fetchData()'</span><span class="sy0">,</span> <span class="nu0">2000</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
window<span class="sy0">.</span>onload <span class="sy0">=</span> initPage<span class="sy0">;</span>
<span class="kw2">&lt;/script&gt;</span>
<span class="sy0">&lt;/</span>head<span class="sy0">&gt;</span>
<span class="sy0">&lt;</span>body<span class="sy0">&gt;</span>
	<span class="sy0">&lt;</span>h1<span class="sy0">&gt;</span>Test page<span class="sy0">&lt;/</span>h1<span class="sy0">&gt;</span>
	<span class="sy0">&lt;</span>p<span class="sy0">&gt;</span>This page is using AJAX to update the <span class="kw3">current</span> <span class="kw3">time</span> every two seconds<span class="sy0">.&lt;/</span>p<span class="sy0">&gt;</span>
	<span class="sy0">&lt;</span>div id<span class="sy0">=</span><span class="st0">&quot;displayCounter&quot;</span><span class="sy0">&gt;&lt;</span>?<span class="sy0">=</span> <span class="kw3">date</span><span class="br0">&#40;</span><span class="st0">&quot;d M Y h:m:s&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span> ?<span class="sy0">&gt;&lt;/</span>div<span class="sy0">&gt;</span>
<span class="sy0">&lt;/</span>body<span class="sy0">&gt;</span>
<span class="sy0">&lt;/</span>html<span class="sy0">&gt;</span>
<span class="kw2">&lt;?</span> <span class="br0">&#125;</span> <span class="kw2">?&gt;</span></pre></div></div></div>

<p>This block of code is PHP and basically tests to see whether a (GET) parameter (called xhttp) was passed in to the URL:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="php" style="color: #000000;"><span class="kw2">&lt;?</span>
<span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">'xhttp'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="re0">$counter</span> <span class="sy0">=</span> <span class="kw3">date</span><span class="br0">&#40;</span><span class="st0">&quot;d M Y h:m:s&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw3">echo</span> <span class="re0">$counter</span><span class="sy0">;</span>
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
<span class="kw2">?&gt;</span></pre></div></div></div>

<p>If the xhttp GET parameter was present in the URL, then the server time is sent back, otherwise the whole HTML page is delivered. This allows us to deliver two very different &#8220;pages&#8221; of data back to the browser using the same single PHP file.</p>
<p>The following block of code is used to output the full name of the PHP page being run (the file name) and merely removes the need to specify the file name in the code itself:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="php" style="color: #000000;"><span class="kw2">&lt;?</span><span class="sy0">=</span> <span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">'SCRIPT_NAME'</span><span class="br0">&#93;</span><span class="sy0">;</span> <span class="kw2">?&gt;</span></pre></div></div></div>

<p>The final block of PHP that is prominent in the page is:</p>

<div class="wp_syntax_outer"><div class="wp_syntax"><div class="wp_syntax_inner"><pre class="php" style="color: #000000;"><span class="kw2">&lt;?</span><span class="sy0">=</span> <span class="kw3">date</span><span class="br0">&#40;</span><span class="st0">&quot;d M Y h:m:s&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="kw2">?&gt;</span></pre></div></div></div>

<p>This writes out the current date and time to the page (and mimics the same date and time format that will be returned if the PHP file is requested with the GET parameter of xhttp added to it).</p>
<p>Everything else is just HTML and Javascript.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecouch.com/2008/10/using-ajax-to-update-the-time-on-a-web-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
