<?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>JerryQu的小站 &#187; header</title>
	<atom:link href="http://www.qgy18.com/tag/header/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.qgy18.com</link>
	<description></description>
	<lastBuildDate>Tue, 16 Mar 2010 03:42:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>利用服务器返回的header来传输数据</title>
		<link>http://www.qgy18.com/2008/10/put-info-into-response-header/</link>
		<comments>http://www.qgy18.com/2008/10/put-info-into-response-header/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 13:13:16 +0000</pubDate>
		<dc:creator>Jerry Qu</dc:creator>
				<category><![CDATA[前端开发]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[header]]></category>

		<guid isPermaLink="false">http://www.qgy18.com/?p=66</guid>
		<description><![CDATA[在Ajax编程时，经常需要从服务端获取数据。通过情况下，我们是直接把要传输的数据放在response正文中，再用responseText或者responseXML来得到内容。最近偶然发现，有时候也可以把数据放在header里，而且一些情况下这样做更有优势。 header是服务器以HTTP协议传送HTML资料到浏览器前所送出的字符串，在php中我们可以这么发送自定义header： header(&#34;author:Jerry Qu&#34;); 然后在客户端，... ]]></description>
			<content:encoded><![CDATA[<p>在Ajax编程时，经常需要从服务端获取数据。通过情况下，我们是直接把要传输的数据放在response正文中，再用responseText或者responseXML来得到内容。最近偶然发现，有时候也可以把数据放在header里，而且一些情况下这样做更有优势。</p>
<p>header是服务器以HTTP协议传送HTML资料到浏览器前所送出的字符串，在php中我们可以这么发送自定义header：</p>
<div class="hl-surround"><div class="hl-main"><span style="color: Blue;">header</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">author:Jerry Qu</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></div></div>
<p>然后在客户端，正常的创建一个Ajax请求，所不同的地方是callback中获取数据改成：</p>
<div class="hl-surround"><div class="hl-main"><span style="color: Green;">var</span><span style="color: Gray;"> </span><span style="color: Blue;">a</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">Ajax</span><span style="color: Olive;">()</span><span style="color: Gray;">;<br /></span><span style="color: Blue;">a</span><span style="color: Gray;">.</span><span style="color: Blue;">get</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">test.php</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;">,</span><span style="color: Green;">function</span><span style="color: Olive;">(){</span><span style="color: Gray;"><br />&nbsp;&nbsp; &nbsp;</span><span style="color: Blue;">alert</span><span style="color: Olive;">(</span><span style="color: Blue;">a</span><span style="color: Gray;">.</span><span style="color: Blue;">req</span><span style="color: Gray;">.</span><span style="color: Blue;">getResponseHeader</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">author</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">))</span><span style="color: Gray;">;<br /></span><span style="color: Olive;">})</span><span style="color: Gray;">;</span></div></div>
<p>这样就能取到author的值了。<span id="more-66"></span></p>
<p>Javascript中跟response header有关的就两个方法：</p>
<blockquote><p>
getResponseHeader<br />
从响应信息中获取指定的http头<br />
语法<br />
strValue = oXMLHttpRequest.getResponseHeader(bstrHeader);</p>
<p>getAllResponseHeaders<br />
获取响应的所有http头<br />
语法<br />
strValue = oXMLHttpRequest.getAllResponseHeaders();
</p></blockquote>
<p>通过header可以传多少数据呢？我测试了一下，在firefox中如果超过10232个英文字符，客户端就取不到数据了，IE中测试了100W个字符依然可以，所以基本还是够用的。另外，我也测试过一次发送1000个自定义header，IE和FF中都能正常取到值。如果你想得到http header的更多信息，建议阅读<a href="http://www.w3.org/Protocols/rfc2616/rfc2616.html">这篇文档</a>。</p>
<p>这样做的优点呢？通常如果用Ajax来post数据，服务端返回json格式字符串的情况下，在浏览器中输入request的地址，用户就会看到那堆奇怪的代码。在页面丢失js时这种现象很常见。但是把返回数据放在header里就不会有这个问题，反正header不会展示出来。正文里可以随意的放些什么内容，哪怕是放一段自动转向JS也没关系，这样用户体验要稍好一些。</p>
<p><a href="http://www.qgy18.com/lab/http-header/">查看本文实例</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.qgy18.com/2008/10/put-info-into-response-header/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
