<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post/2683/</link>
<title><![CDATA[关于跨域问题]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 04 Feb 2010 06:36:04 +0000</pubDate> 
<guid>http://www.jackxiang.com/post/2683/</guid> 
<description>
<![CDATA[ 
	转自：温德馨的博客，哈哈，向东的博客早就O了，链接还是xiangdong.org，哎，我转了一席，他对mysql的io调度和DAS、NAS、SAN存储系统完全分析都有研究和关注，哈哈<br/><br/><br/>一直对JS的跨域概念很迷惑，记得是在刚学ajax的时候才了解了一下这个概念，后来就再没有接触。主要工作上是做PHP为主，前端的东西几乎不需要插手。今天看了膘叔的一篇文章《孟宪会之AJAX（XMLHttpRequest）进行跨域请求方法详解》，一直是知道跨域会存在问题，但是自己就没有动过手体验一下具体会造成什么问题，于是决定试验一下。在blog服务器上写了个test.php测试脚本<br/><div class="code"><br/>&nbsp;&nbsp;&lt;?php<br/>&nbsp;&nbsp;$data = array(&#039;status&#039; =&gt; 1, ‘msg’ =&gt; ‘hello’);<br/>&nbsp;&nbsp;echo json_encode($data);<br/>&nbsp;&nbsp;?&gt;&lt;/p&gt;<br/>&nbsp;&nbsp;&lt;p&gt;<br/></div><br/>前端测试页面<br/><br/><div class="code"><br/>&nbsp;&nbsp;&lt;script&gt;<br/>&nbsp;&nbsp;var xhr = new XMLHttpRequest();<br/>&nbsp;&nbsp;var url = &quot;http://jackywdx.cn/test.php&quot;;<br/>&nbsp;&nbsp;function test()&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(xhr)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xhr.open(&#039;GET&#039;, url, true);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xhr.onreadystatechange&nbsp;&nbsp;= handler;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xhr.send();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;function handler(evtxhr)<br/>&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(xhr.readyState == 4)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(xhr.status == 200)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var re = xhr.responseText;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(re);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(&#039;status not 200&#039;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(xhr.status);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(&#039;readyState not 4&#039;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(xhr.readyState);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&#125;&lt;/p&gt;<br/>&nbsp;&nbsp;&lt;p&gt;&lt;body onload=&quot;test()&quot;&gt;<br/>&nbsp;&nbsp;hello<br/>&nbsp;&nbsp;&lt;/body&gt;<br/></div><br/><br/>在本地访问，http://test.cn/test.html 结果是失败，没有alert出responseText; 在test.php文件里面增加一句： header(”Access-Control-Allow-Origin:http://test.cn”); 这样就可以了。我用的FF版本是3.5.7，所以支持这个Access-Control-Allow-Origin HTTP头部参数。用HTTPFOX抓包看到结果：<br/><br/>(Request-Line)<br/><br/>GET /test.php HTTP/1.1<br/><br/>Host jackywdx.cn<br/><br/>User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7<br/><br/>Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8<br/><br/>Accept-Language zh-cn,zh;q=0.5<br/><br/>Accept-Encoding gzip,deflate<br/><br/>Accept-Charset GB2312,utf-8;q=0.7,*;q=0.7<br/><br/>Keep-Alive 300<br/><br/>Connection keep-alive<br/><br/>Referer http://test.cn/test.html<br/><br/>Origin http://test.cn<br/><br/>(Status-Line) HTTP/1.1 200 OK<br/><br/>Server nginx/0.7.61<br/><br/>Date Fri, 29 Jan 2010 02:43:39 GMT<br/><br/>Content-Type text/html<br/><br/>Transfer-Encoding chunked<br/><br/>Connection close<br/><br/>X-Powered-By PHP/5.2.6<br/><br/>Access-Control-Allow-Origin http://test.cn<br/><br/>Author jackywdx<br/><br/>X-Via US_RASHOST_VPS<br/><br/>Content-Encoding gzip<br/><br/>解决JS跨域问题还有一些解决办法：<br/><br/>如果是同一主域的话可以设置document.domain<br/><br/>另外还可以通过flash来作为中转角色，flash跨域可以通过crossdomain.xml来解决，不过这种方法速度会比较慢。毕竟多了一层中间层。<br/><br/>还有一种比较土的方法就是在相册的域下面做一个同样的接口-.-
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post/2683/#blogcomment51515</link>
<title><![CDATA[[评论] 关于跨域问题]]></title> 
<author>孩子 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 04 Feb 2010 07:01:04 +0000</pubDate> 
<guid>http://www.jackxiang.com/post/2683/#blogcomment51515</guid> 
<description>
<![CDATA[ 
	留爪。
]]>
</description>
</item>
</channel>
</rss>