<?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>随网之舞缓存</title>
	<atom:link href="http://dancewithnet.com/tag/%e7%bc%93%e5%ad%98/feed/" rel="self" type="application/rss+xml" />
	<link>http://dancewithnet.com</link>
	<description>WEB Design{HTML:CSS:Javascript:DOM:AJAX:PHP:Usability}/Viewpoint/Photo/Food/Mood</description>
	<lastBuildDate>Mon, 18 Jul 2011 02:45:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ajax的小贴士</title>
		<link>http://dancewithnet.com/2007/09/25/ajax-tips/</link>
		<comments>http://dancewithnet.com/2007/09/25/ajax-tips/#comments</comments>
		<pubDate>Tue, 25 Sep 2007 10:14:15 +0000</pubDate>
		<dc:creator>秦歌</dc:creator>
				<category><![CDATA[Javascript & DOM & AJAX]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[gb2312]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[responseXML]]></category>
		<category><![CDATA[utf-8]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[编码]]></category>

		<guid isPermaLink="false">http://dancewithnet.com/2007/09/25/ajax-tips/</guid>
		<description><![CDATA[在使用Ajax过程中，有时候总会遇到一些难题，浏览器兼容、编码、IE下的特殊处理等等，偶尔会搞的人头昏脑胀哭笑不得，这里列一些小贴士，或许有些用。
使用Javascript库
Ajax的流行和巨大威... ]]></description>
			<content:encoded><![CDATA[<p>在使用Ajax过程中，有时候总会遇到一些难题，浏览器兼容、编码、IE下的特殊处理等等，偶尔会搞的人头昏脑胀哭笑不得，这里列一些小贴士，或许有些用。</p>
<h3>使用Javascript库</h3>
<p>Ajax的流行和巨大威力，让我们重新审视了Javascript的开发，也直接促使各种库的出现。对于普通的开发者，使用一些适合自己的Javascript库不仅可以避免Ajax应用上的浏览器兼容等问题，也使其开发更加的稳定和高效。这里列一些我知晓的轻量级的Javascript库：<span id="more-1135"></span></p>
<ol>
<li><strong><a href="http://developer.yahoo.com/yui/">YUI</a></strong>：YAHOO出品，组件丰富强大健壮稳定，是团队协作开发的首选。</li>
<li><strong><a href="http://jquery.com/">JQuery</a></strong>：灵活、高效，其基于CSS3 和XPath的选择器语法引擎非常的强大和完整。</li>
<li><strong><a href="http://www.prototypejs.org/">Prototype</a></strong>：是一个非常优雅的Javascript库，最经典的莫过于$符号了，DWR，JQuery都被它吸引了。在它基础上出现了<a href="http://script.aculo.us/">script.aculo.us</a>。</li>
<li><strong><a href="http://mootools.net/">Mootools</a></strong>：核心语法和Prototype比较类似，但是用过之后才知道什么叫简单轻巧和短小精悍。</li>
</ol>
<h3>编码问题</h3>
<p>通过XMLHttpRequest获取的数据，默认的字符编码是UTF-8，如果前端页面是GB2312或者其它编码，显示获取的数据就是乱码。通过XMLHTTPRequest，POST的数据也是UTF-8编码，如果后台是GB2312或者其他编码也会出现乱码。解决方法：</p>
<ol>
<li>统一到UTF-8。这也是国际化的必然趋势。</li>
<li>输出通过XMLHttpRequest获取的文本文本时，在headers中增加文本声明(直接HTML声明没有作用)。如：
<pre><code>PHP:header('Content-Type:text/html;charset=GB2312');
ASP:Response.Charset = "GB2312"
JSP:response.setHeader("Charset","GB2312");</code></pre>
</li>
<li>WWW服务器上强制声明。比如：apache下的配置：<code>AddDefaultCharset GB2312</code>这种情况主要是应对通过XMLHttpRequest访问的文件是静态文件，无法声明headers的情况下。<br />&nbsp;&nbsp;静态页面一般都会经过Apache的deflate或gzip压缩，此时在上面情况下IE中，首次通过XMLhttpRequest获得的数据可以正常显示，但再获取数据显示时出现乱码，这次因为再次获取的数据来自缓存，可能由于浏览器解压缩的问题导致Apache设置的默认编码声明丢失。由于这种情况下一般是纯文本，可能还无法禁止缓存，可以设置XMLhttpRequest访问的文本文件不压缩来解决这个问题。</p>
<li>非UTF-8页面通过XMLHttpRequest获取的文本文本输出前字符转码成unicode，或者编码直接是UTF-8，可以正常显示。<strong>如<a href="http://dancewithnet.com/lab/2007/09/ajax-tips/" class="link-btn">实例</a>所示。</strong></li>
</ol>
<h3>IE下的缓存问题</h3>
<p>由于IE的缓存处理机制问题，每次通过XMLHttpRequest访问动态页面返回的总是首次访问的内容，解决方法有：</p>
<ol>
<li>客户端通过添加随机字符串解决。如：
<pre><code>var url = 'http://dancewithnet.com/';
url +=  '?temp=' + new Date().getTime();
url +=  '?temp=' + Math.random();
</code></pre>
</li>
<li>在HTTP headers禁止缓存。如：
<pre><code>
HTTP:
&lt;meta http-equiv="pragma" content="no-cache" /&gt;
&lt;meta http-equiv="Cache-Control" content="no-cache, must-revalidate" /&gt;
&lt;meta http-equiv="expires" content="Thu, 01 Jan 1970 00:00:01 GMT" /&gt;
&lt;meta http-equiv="expires" content="0" /&gt;

PHP：
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

ASP：
Response.expires=0
Response.addHeader("pragma","no-cache")
Response.addHeader("Cache-Control","no-cache, must-revalidate")

JSP：
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Expires", "Thu, 01 Jan 1970 00:00:01 GMT");
</code></pre>
</li>
<li>在XMLHttpRequest发送请求之前加上：
<pre><code>XMLHttpRequest.setRequestHeader("If-Modified-Since","0");
XMLHttpRequest.send(null);
</code></pre>
</li>
</ol>
<h3>IE下的reponseXML问题</h3>
<p>使用responseXML时，IE下只能接受.xml为后缀的XML文件，如果不能以.xml文件为结尾的，则需要如下处理：</p>
<ol>
<li>在服务器端声明是xml文件类型。如
<pre><code>
PHP:header("Content-Type:text/xml;charset=utf-8");
ASP:Response.ContentType = "text/xml";
JSP:response.setHeader("ContentType","text/xml");</code></pre>
<p></code></pre>
</li>
<li>利用responseText获取，然后封装成XML。</li>
<li>在AJAX应用上，<a href="http://json.org/">JSON</a>和<a href="http://jsonml.org/">JsonML</a>是XML非常好的替代品。</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://dancewithnet.com/2007/09/25/ajax-tips/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

