<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[刘新修]]></title> 
<link>http://pic1.liuxinxiu.com:80/index.php</link> 
<description><![CDATA[刘新修的个人博客 (Liuxinxiu'S Blog)]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[刘新修]]></copyright>
<item>
<link>http://pic1.liuxinxiu.com:80/s/333/</link>
<title><![CDATA[解决Chrome, NET::ERR_CERT_AUTHORITY_INVALID]]></title> 
<author>刘新修 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[HTTP相关]]></category>
<pubDate>Thu, 06 Jun 2024 02:37:41 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/s/333/</guid> 
<description>
<![CDATA[ 
	<p>&nbsp;<img src="/attachment/2024/06/image/20201108085742867.png" width="470" height="432" alt="" /></p><h3 style="box-sizing: border-box; outline: 0px; margin: 24px 0px 8px; padding: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, SimHei, Arial, SimSun; font-size: 18px; color: rgb(79, 79, 79); line-height: 28px; font-synthesis-style: auto; overflow-wrap: break-word; font-variant-ligatures: no-common-ligatures;">解决方法一：</h3><p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(77, 77, 77); line-height: 26px; overflow: auto hidden; font-synthesis-style: auto; overflow-wrap: break-word; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-variant-ligatures: no-common-ligatures;">首先清理一下缓存。三个点-&gt;设置-&gt;清除浏览数据，即可。</p><p><img src="/attachment/2024/06/image/20201108090037655.png" width="287" height="415" alt="" /></p><p><img src="/attachment/2024/06/image/20201108085935642.png" width="941" height="387" alt="" /></p><p><span style="color: rgb(77, 77, 77); font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-size: 16px; font-variant-ligatures: no-common-ligatures;">如果还解决不了，因为Chrome是默认使用HSTS传输，严格的http传输方式。</span></p><h3 style="box-sizing: border-box; outline: 0px; margin: 24px 0px 8px; padding: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, SimHei, Arial, SimSun; font-size: 18px; color: rgb(79, 79, 79); line-height: 28px; font-synthesis-style: auto; overflow-wrap: break-word; font-variant-ligatures: no-common-ligatures;">解决方法二：</h3><p>&nbsp;</p><p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(77, 77, 77); line-height: 26px; overflow: auto hidden; font-synthesis-style: auto; overflow-wrap: break-word; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-variant-ligatures: no-common-ligatures;">①在Chrome浏览框输入<br style="box-sizing: border-box; outline: 0px; font-synthesis-style: auto; overflow-wrap: break-word;" />chrome://net-internals/#hsts<br style="box-sizing: border-box; outline: 0px; font-synthesis-style: auto; overflow-wrap: break-word;" />查询域名google.com的内容</p><p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(77, 77, 77); line-height: 26px; overflow: auto hidden; font-synthesis-style: auto; overflow-wrap: break-word; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-variant-ligatures: no-common-ligatures;"><img src="/attachment/2024/06/image/202011080903494.png" width="529" height="200" alt="" /></p><p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(77, 77, 77); line-height: 26px; overflow: auto hidden; font-synthesis-style: auto; overflow-wrap: break-word; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-variant-ligatures: no-common-ligatures;">②删除安全证书</p><p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(77, 77, 77); line-height: 26px; overflow: auto hidden; font-synthesis-style: auto; overflow-wrap: break-word; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-variant-ligatures: no-common-ligatures;"><img src="/attachment/2024/06/image/20201108090615611.png" width="736" height="95" alt="" /></p><p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; --el-button-hover-bg-color: #6D5FFD; --el-button-hover-border-color: #6D5FFD; --el-button-active-bg-color: #6D5FFD; --el-button-active-border-color: #6D5FFD; font-size: 16px; color: rgb(77, 77, 77); line-height: 26px; overflow: auto hidden; font-synthesis-style: auto; overflow-wrap: break-word; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-variant-ligatures: no-common-ligatures;">完美解决。</p><h3 style="box-sizing: border-box; outline: 0px; margin: 24px 0px 8px; padding: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, SimHei, Arial, SimSun; font-size: 18px; color: rgb(79, 79, 79); line-height: 28px; font-synthesis-style: auto; overflow-wrap: break-word; font-variant-ligatures: no-common-ligatures;"><a name="t4" style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; font-weight: normal; cursor: pointer; background-color: transparent; color: rgb(78, 161, 219); font-synthesis-style: auto; overflow-wrap: break-word;"></a>总结</h3><p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(77, 77, 77); line-height: 26px; overflow: auto hidden; font-synthesis-style: auto; overflow-wrap: break-word; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif; font-variant-ligatures: no-common-ligatures;">这是因为https网站被劫持了，然后Chrome又默认使用HSTS，才导致无法访问。通过上述方法可以解决网站被劫持现象，像之前那些在该浏览器直接敲打thisisunsafe的方法，不怎么见效。</p><br/>Tags - <a href="http://pic1.liuxinxiu.com:80/tags/http/" rel="tag">http</a> , <a href="http://pic1.liuxinxiu.com:80/tags/https/" rel="tag">https</a>
]]>
</description>
</item><item>
<link>http://pic1.liuxinxiu.com:80/Webbench_install_MAC/</link>
<title><![CDATA[Webbench MAC下安装]]></title> 
<author>刘新修 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[HTTP相关]]></category>
<pubDate>Sat, 10 Jun 2017 08:57:58 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/Webbench_install_MAC/</guid> 
<description>
<![CDATA[ 
	<p><span style="color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">Webbench是有名的网站压力</span><a href="http://lib.csdn.net/base/softwaretest" class="replace_word" title="软件测试知识库" target="_blank" style="font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; color: rgb(223, 52, 52); text-decoration-line: none; font-weight: bold;">测试</a><span style="color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">工具，它是由 Lionbridge公司开发。</span></p><div style="color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin-bottom: 30px; line-height: 31px;">Webbech能测试处在相同硬件上，不同服务的性能以及不同硬件上同一个服务的运行状况。webBech的标准测试可以向我们展示服务器的两项内容：每秒钟相应请求数和每秒钟传输数据量。webbench不但能具有便准静态页面的测试能力，还能对动态页面（ASP,<a href="http://lib.csdn.net/base/php" class="replace_word" title="PHP知识库" target="_blank" style="color: rgb(223, 52, 52); text-decoration-line: none; font-weight: bold;">PHP</a>,<a href="http://lib.csdn.net/base/java" class="replace_word" title="Java 知识库" target="_blank" style="color: rgb(223, 52, 52); text-decoration-line: none; font-weight: bold;">Java</a>,CGI）进 行测试的能力。还有就是他支持对含有SSL的安全网站例如电子商务网站进行静态或动态的性能测试。</p><p><span style="font-family: inherit;">安装</span>: Make install 需 <span style="font-weight: 700;">sudo</span> 权限, 需要写入 /usr/local/bin</p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>brew install ctags # 依赖安装&nbsp;&nbsp;</span></span></li><li><span>curl -o webbench.tar.gz http:<span class="comment">//blog.zyan.cc/soft/linux/webbench/webbench-1.5.tar.gz</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>tar -zxvf webbench-1.5.tar.gz&nbsp;&nbsp;</span></li><li><span>cd webbench-1.5/&nbsp;&nbsp;</span></li><li class="alt"><span>mkdir -pv /usr/local/man/man1 # 关键&nbsp;&nbsp;</span></li><li><span>sudo make &amp;&amp; sudo make install # sudo 权限因为需要创建文件夹&nbsp;&nbsp;</span></li></ol></div></div><p>Mac下自带apache压力测试：</p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>localhost:webbench-1.5&nbsp;jesse$&nbsp;webbench&nbsp;-c&nbsp;500&nbsp;-t&nbsp;120&nbsp;http:</span><span class="comment">//localhost/</span><span>&nbsp;&nbsp;</span></span></li><li><span>Webbench&nbsp;-&nbsp;Simple&nbsp;Web&nbsp;Benchmark&nbsp;1.5&nbsp;&nbsp;</span></li><li class="alt"><span>Copyright&nbsp;(c)&nbsp;Radim&nbsp;Kolar&nbsp;1997-2004,&nbsp;GPL&nbsp;Open&nbsp;Source&nbsp;Software.&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>Benchmarking:&nbsp;GET&nbsp;http:<span class="comment">//localhost/</span><span>&nbsp;&nbsp;</span></span></li><li><span>500&nbsp;clients,&nbsp;running&nbsp;120&nbsp;sec.&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li><span>Speed=33001&nbsp;pages/min,&nbsp;696314&nbsp;bytes/sec.&nbsp;&nbsp;</span></li><li class="alt"><span>Requests:&nbsp;64078&nbsp;susceed,&nbsp;1924&nbsp;failed.&nbsp;&nbsp;</span></li><li><span>localhost:webbench-1.5&nbsp;jesse$&nbsp;&nbsp;&nbsp;</span></li></ol></div><p>&nbsp;</p>
]]>
</description>
</item><item>
<link>http://pic1.liuxinxiu.com:80/http-200-from-cache-vs-304-not-modified/</link>
<title><![CDATA[http 200 from cache vs 304 not modified]]></title> 
<author>刘新修 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[HTTP相关]]></category>
<pubDate>Sun, 18 Sep 2016 11:06:29 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/http-200-from-cache-vs-304-not-modified/</guid> 
<description>
<![CDATA[ 
	<p>在讨论这个问题之前先了解一下浏览器的缓存机制。浏览器的缓存机制有两种，一种是&ldquo;Freshness（新鲜度，过期机制）&rdquo;，另一种是&ldquo;Validation（校验值，验证机制）&rdquo;。</p><div>&nbsp;</div><div>&ldquo;Freshness（新鲜度，过期机制）&rdquo;：缓存副本有效期。若一个缓存副本是有效的、足够新的，则在这段有效期，浏览器直接从缓存中返回数据，无需再次发出实际请求。于是就有了&ldquo;http 200 from cache&rdquo;。</div><div>&nbsp;</div><div>&ldquo;Validation（校验值，验证机制）&rdquo;：http设定了一套校验规则，用来校验当前客户端所缓存的资源是否仍然是新鲜的。当缓存失效时（Freshness优先级高于Validation），浏览器会在请求头中包含前置条件头，再次向服务器发送请求。当校验失效时，说明资源已经被修改或过期，浏览器需求重新获取资源内容。当校验未失效时，会返回&ldquo;304 not modified&rdquo;。</div><div>&nbsp;</div><div>与缓存有关的http消息报头</div><div>&nbsp;</div><div>1.Freshness（新鲜度，过期机制）有关的http消息报头有Cache-Control、Expires、Pragma等。</div><div>&nbsp;</div><div>Cache-Control与Expires作用一致，都是指明当前资源的有效期，控制浏览器是否直接从浏览器缓存取数据还是重新发请求到服务器取数据。只不过Cache-Control的选择更多，设置更细致，如果同时设置的话，其优先级高于Expires。</div><div>&nbsp;</div><div>2.Validation（校验值，验证机制）有关的http消息报头有ETag、Last-Modified、If-Modified-Since、If-None-Match、If-Range、Cache-Control等。</div><div>&nbsp;</div><div>Last-modified 和 If-Modefied-Since 一起工作。Last-modfied（在request头内） 指的是本地文件的修改时间。 If-Modefied-Since（在response头内）指的是服务器文件的修改时间。当Last-modefied与If-modefied-since一致时，说明文件没有被修改过，返回304，浏览器直接读取缓存中的文件。若不一致，则说明文件被修改过，返回200，浏览器重新从服务器换取文件。</div><div>&nbsp;</div><div>If-None-Match 和 ETag 一起工作。 如果If-None-Match 和 ETag 一致，说明文件没有修改，返回304，使用缓存。否则重新从服务器获取文件。</div><div>&nbsp;</div><div>既然已经有了 Last-Modified 已经能够知道本地缓存是否是最新的了，为什么还需要 Etag 呢？</div><div>&nbsp;</div><div>主要是基于以下几个原因：</div><div>&nbsp;</div><div>Last-Modified 标注的最后修改时间只能精确到秒，如果有些资源在一秒之内被多次修改的话，他就不能准确标注文件的新鲜度了</div><div>如果某些资源会被定期生成，当内容没有变化，但 Last-Modified 却改变了，导致文件没使用缓存</div><div>有可能存在服务器没有准确获取资源修改时间，或者与代理服务器时间不一致的情形。</div><div>Last-Modified与ETag是可以一起使用的，服务器会优先验证ETag，一致的情况下，才会继续比对Last-Modified，最后才决定是否返回304</div><div>=============================================================</div><div><div>1,Last-Modified</div><div>设置 header(&quot;Last-Modified: &quot;.gmdate(&quot;D, d M Y H:i:s&quot;, time() ).&quot; GMT&quot;);&nbsp;</div><div>Last-Modified虽然使用了缓存，但是每次打开页面依然需要向服务器发起http请求，浏览器根据用户的$_SERVER['HTTP_IF_MODIFIED_SINCE']来判断浏览器的内容是否过期，没过期的话返回304状态，浏览器内容从缓存中读取</div><div>&nbsp;</div><div>2，Expires</div><div>设置 header(&quot;Expires: &quot;.gmdate(&quot;D, d M Y H:i:s&quot;, time()+$cache_time ).&quot; GMT&quot;);&nbsp;</div><div>状态码依然是200，时间依然是旧的时间，Size栏目显示为from cache，表示内容是直接从浏览器读取，浏览器根本就没有向服务器发起http请求</div><div>&nbsp;</div><div>Expires比Last-Modified缓存效果更好是吧，因为本地有缓存数据时，不需要向服务器发起http请求，服务器的并发数会明显的减少，可以少处理很多http请求</div></div><p>&nbsp;</p>
]]>
</description>
</item><item>
<link>http://pic1.liuxinxiu.com:80/CORS/</link>
<title><![CDATA[CORS跨域资源共享 (Cross-origin resource sharing)]]></title> 
<author>刘新修 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[HTTP相关]]></category>
<pubDate>Mon, 12 Sep 2016 03:30:43 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/CORS/</guid> 
<description>
<![CDATA[ 
	<p>&nbsp;CORS是一个W3C标准，全称是&quot;跨域资源共享&quot; (Cross-origin resource sharing)</p><div>CORS，是一种服务器端的跨域解决方案，这种方案对于跨域的解决代码均在服务器端设置完成。在服务器端接收到客户端的请求后，通过获取到请求的来源站点Origin，如果愿意将资源共享给该站点，则通过设置&ldquo;Access-Control-Allow-Origin&rdquo;报头的值为请求的来源站点。浏览器接收到包含资源的响应后，会提取&ldquo;Access-Control-Allow-Origin&rdquo;响应报头的值，如果此值为*或者包含的源列表包含请求的源站点，则浏览器会允许JavaScript操作获取的资源；否则，不会操作获取的资源。</div><div>&nbsp;</div><div>跨域资源共享（CORS ）是一种网络浏览器的技术规范，CORS系统定义了一种浏览器和服务器交互的方式来确定是否允许跨域请求。 它是一个妥协，有更大的灵活性，但比起简单地允许所有这些的要求来说更加安全。</div><div>&nbsp;</div><div>简言之，CORS就是为了让AJAX可以实现可控的跨域访问而生的。</div><div><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>以往的解决方案</strong></p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;以前要实现跨域访问，可以通过JSONP、Flash或者服务器中转的方式来实现，但是现在我们有了CORS。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;CORS与JSONP相比，无疑更为先进、方便和可靠。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;1、 JSONP只能实现GET请求，而CORS支持所有类型的HTTP请求。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;2、 使用CORS，开发者可以使用普通的XMLHttpRequest发起请求和获得数据，比起JSONP有更好的错误处理。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;3、 JSONP主要被老的浏览器支持，它们往往不支持CORS，而绝大多数现代浏览器都已经支持了CORS（这部分会在后文浏览器支持部分介绍）。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>详细内容</strong></p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;要使用CORS，我们需要了解前端和服务器端的使用方法。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;1、&nbsp; 前端</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;以前我们使用Ajax，代码类似于如下的方式：</p><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; overflow: auto; word-break: break-word; margin: 0px;"><ol class="dp-xml" start="1" style="padding-left: 40px; border: none; color: rgb(92, 92, 92); margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">var&nbsp;</span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">xhr</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;XMLHttpRequest();&nbsp;&nbsp;</span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">xhr.open(&quot;GET&quot;,&nbsp;&quot;/hfahe&quot;,&nbsp;true);&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">xhr.send(); &nbsp;</span></li></ol></div><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;这里的&ldquo;/hfahe&rdquo;是本域的相对路径。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;如果我们要使用CORS，相关Ajax代码可能如下所示：</p><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; overflow: auto; word-break: break-word; margin: 0px;"><ol class="dp-xml" start="1" style="padding-left: 40px; border: none; color: rgb(92, 92, 92); margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">var&nbsp;</span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">xhr</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;XMLHttpRequest();&nbsp;&nbsp;</span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">xhr.open(&quot;GET&quot;,&nbsp;&quot;http://www.liuxinxiu.com/hfahe&quot;,&nbsp;true);&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">xhr.send(); &nbsp;</span></li></ol></div><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;请注意，代码与之前的区别就在于相对路径换成了其他域的绝对路径，也就是你要跨域访问的接口地址。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;我们还必须提供浏览器回退功能检测和支持，避免浏览器不支持的情况。</p><div style="margin: 0px; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; overflow: auto; word-break: break-word; margin: 0px;"><ol class="dp-xml" start="1" style="padding-left: 40px; border: none; color: rgb(92, 92, 92); margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">function&nbsp;createCORSRequest(method,&nbsp;url)&nbsp;&#123;&nbsp;&nbsp;</span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;var&nbsp;<span class="attribute" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">xhr</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;XMLHttpRequest();&nbsp;&nbsp;</span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;if&nbsp;(&quot;withCredentials&quot;&nbsp;in&nbsp;xhr)&nbsp;&#123;&nbsp;&nbsp;</span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;此时即支持CORS的情况&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;检查XMLHttpRequest对象是否有&ldquo;withCredentials&rdquo;属性&nbsp;&nbsp;</span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&ldquo;withCredentials&rdquo;仅存在于XMLHTTPRequest2对象里&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;xhr.open(method,&nbsp;url,&nbsp;true);&nbsp;&nbsp;</span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&#125;&nbsp;else&nbsp;if&nbsp;(typeof!=&nbsp;&quot;undefined&quot;)&nbsp;&#123;&nbsp;&nbsp;</span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;否则检查是否支持XDomainRequest，IE8和IE9支持&nbsp;&nbsp;</span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;XDomainRequest仅存在于IE中，是IE用于支持CORS请求的方式&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="attribute" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">xhr</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;XDomainRequest();&nbsp;&nbsp;</span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;xhr.open(method,&nbsp;url);&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;</span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&#125;&nbsp;else&nbsp;&#123;&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;</span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;否则，浏览器不支持CORS&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="attribute" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">xhr</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">null</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">;&nbsp;&nbsp;</span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&#125;&nbsp;&nbsp;</span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;return&nbsp;xhr;&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&#125;&nbsp;&nbsp;</span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">var&nbsp;<span class="attribute" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">xhr</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">createCORSRequest</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">('GET',&nbsp;url);&nbsp;&nbsp;</span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">if&nbsp;(!xhr)&nbsp;&#123;&nbsp;&nbsp;</span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;&nbsp;throw&nbsp;new&nbsp;Error('CORS&nbsp;not&nbsp;supported');&nbsp;&nbsp;</span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&#125;&nbsp;&nbsp;</span></li></ol></div></div><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;现在如果直接使用上面的脚本进行请求，会看到浏览器里控制台的报错如下：</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://at.liuxinxiu.com/2016/09/image/1.jpg" width="558" height="201" alt="" /></p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;错误显示的很明显，这是因为我们还未设置Access-Control-Allow-Origin头。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;2、&nbsp; 服务器</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;服务器端对于CORS的支持，主要就是通过设置Access-Control-Allow-Origin来进行的。如果浏览器检测到相应的设置，就可以允许Ajax进行跨域的访问。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;HTTP 头的设置方法有很多，<a href="http://enable-cors.org/" style="color: rgb(255, 51, 102); text-decoration: none;">http://enable-cors.org/</a>这篇文章里对各种服务器和语言的设置都有详细的介绍，下面我们主要介绍Apache和PHP里的设置方法。</p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Apache：Apache需要使用mod_headers模块来激活HTTP头的设置，它默认是激活的。你只需要在Apache配置文件的&lt;Directory&gt;,&nbsp;&lt;Location&gt;,&nbsp;&lt;Files&gt;或&lt;VirtualHost&gt;的配置里加入以下内容即可：</p><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; overflow: auto; word-break: break-word; margin: 0px;"><ol class="dp-xml" start="1" style="padding-left: 40px; border: none; color: rgb(92, 92, 92); margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">Header&nbsp;set&nbsp;Access-Control-Allow-Origin&nbsp;* &nbsp;</span></span></li></ol></div><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;PHP：只需要使用如下的代码设置即可。</p><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; overflow: auto; word-break: break-word; margin: 0px;"><ol class="dp-xml" start="1" style="padding-left: 40px; border: none; color: rgb(92, 92, 92); margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&lt;?</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">php</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;&nbsp;</span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;">&nbsp;header(&quot;Access-Control-Allow-Origin:*&quot;); &nbsp;</span></li></ol></div><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;以上的配置的含义是允许任何域发起的请求都可以获取当前服务器的数据。当然，这样有很大的危险性，恶意站点可能通过XSS攻击我们的服务器。所以我们应该尽量有针对性的对限制安全的来源，例如下面的设置使得只有http://www.liuxinxiu.com 这个域才能跨域访问服务器的API。</p><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; overflow: auto; word-break: break-word; margin: 0px;"><ol class="dp-xml" start="1" style="padding-left: 40px; border: none; color: rgb(92, 92, 92); margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal outside; color: inherit; line-height: 1.6; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 0, 0); background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">Access-Control-Allow-Origin:&nbsp;http://www.liuxinxiu.com &nbsp;</span></span></li></ol></div><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>浏览器支持情况</strong></p><p style="margin: 10px auto; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong><img src="http://at.liuxinxiu.com/2016/09/image/2.jpg" width="480" height="295" alt="" /></strong></p></div><br/>Tags - <a href="http://pic1.liuxinxiu.com:80/tags/cors/" rel="tag">cors</a>
]]>
</description>
</item><item>
<link>http://pic1.liuxinxiu.com:80/form-data/</link>
<title><![CDATA[HTTP协议之multipart/form-data请求分析]]></title> 
<author>刘新修 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[HTTP相关]]></category>
<pubDate>Mon, 12 Sep 2016 02:39:46 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/form-data/</guid> 
<description>
<![CDATA[ 
	<p>&nbsp;&nbsp;<span style="color: rgb(85, 85, 85); font-family: Arial; line-height: 26px;">首先来了解什么是multipart/form-data请求：</span></p><p style="margin-bottom: 0px; color: rgb(85, 85, 85); font-family: Arial; line-height: 26px;">根据<a target="_blank" href="http://www.faqs.org/rfcs/rfc2616.html" style="color: rgb(202, 0, 0); text-decoration: none;"><em>http</em>/<em>1.1</em>&nbsp;rfc 2616</a>的协议规定，我们的请求方式只有OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACE等，那为为何我们还会有multipart/form-data请求之说呢？这就要从头来说了。</p><p style="margin-bottom: 0px; color: rgb(85, 85, 85); font-family: Arial; line-height: 26px;">http协议大家都知道是规定了以ASCII码传输，建立在tcp、ip协议之上的应用层规范，规范内容把http请求分为3个部门：状态行，请求头，请求体。所有的方法、实现都是围绕如何运用和组织这三部分来完成的。换句话来说就是万变不离其中，只要我们了解了http请求的组成部分后，自然就可以应变任何实际工作中的需求和问题了。</p><p style="margin-bottom: 0px; color: rgb(85, 85, 85); font-family: Arial; line-height: 26px;">关于状态行，请求头，请求体等三部分的具体内容，大家可以参考官方的协议文档http://www.faqs.org/rfcs/rfc2616.html，这里主要分析multipart/form-data请求具体是怎么一回事。</p><p style="margin-bottom: 0px; color: rgb(85, 85, 85); font-family: Arial; line-height: 26px;">既然http协议本身的原始方法不支持multipart/form-data请求，那这个请求自然就是由这些原始的方法演变而来的，具体如何演变且看下文：</p><p style="margin-bottom: 0px; color: rgb(85, 85, 85); font-family: Arial; line-height: 26px;">1、multipart/form-data的基础方法是post，也就是说是由post方法来组合实现的</p><p style="margin-bottom: 0px; color: rgb(85, 85, 85); font-family: Arial; line-height: 26px;">2、multipart/form-data与post方法的不同之处：请求头，请求体。</p><p style="margin-bottom: 0px; color: rgb(85, 85, 85); font-family: Arial; line-height: 26px;">3、multipart/form-data的请求头必须包含一个特殊的头信息：Content-Type，且其值也必须规定为multipart/form-data，同时还需要规定一个内容分割符用于分割请求体中的多个post的内容，如文件内容和文本内容自然需要分割开来，不然接收方就无法正常解析和还原这个文件了。具体的头信息如下：</p><div class="codeText" style="width: 759.5px;"><div class="codeHead">JavaScript代码</div><ol start="1" class="dp-c"><li class="alt">Content-Type:&nbsp;multipart/form-data;&nbsp;boundary=$&#123;bound&#125;&nbsp;&nbsp;</li></ol></div><p style="margin-bottom: 0px; font-family: Arial; line-height: 26px;">//其中$&#123;bound&#125; 是一个占位符，代表我们规定的分割符，可以自己任意规定，但为了避免和正常文本重复了，尽量要使用复杂一点的内容。如：--------------------56423498738365<br />4、multipart/form-data的请求体也是一个字符串，不过和post的请求体不同的是它的构造方式，post是简单的name=value值连接，而multipart/form-data则是添加了分隔符等内容的构造体。具体格式如下:</p><div class="codeText" style="width: 759.5px;"><div class="codeHead">JavaScript代码</div><ol start="1" class="dp-c"><li class="alt">--$&#123;bound&#125;&nbsp;&nbsp;&nbsp;&nbsp;</li><li>Content-Disposition:&nbsp;form-data;&nbsp;name=<span class="string">&quot;Filename&quot;</span>&nbsp;&nbsp;&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;</li><li>HTTP.pdf&nbsp;&nbsp;&nbsp;&nbsp;</li><li class="alt">--$&#123;bound&#125;&nbsp;&nbsp;&nbsp;&nbsp;</li><li>Content-Disposition:&nbsp;form-data;&nbsp;name=<span class="string">&quot;file000&quot;</span>;&nbsp;filename=<span class="string">&quot;HTTP协议详解.pdf&quot;</span>&nbsp;&nbsp;&nbsp;&nbsp;</li><li class="alt">Content-Type:&nbsp;application/octet-stream&nbsp;&nbsp;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;</li><li class="alt">%PDF-1.5&nbsp;&nbsp;&nbsp;&nbsp;</li><li>file&nbsp;content&nbsp;&nbsp;&nbsp;&nbsp;</li><li class="alt">%%EOF&nbsp;&nbsp;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;</li><li class="alt">--$&#123;bound&#125;&nbsp;&nbsp;&nbsp;&nbsp;</li><li>Content-Disposition:&nbsp;form-data;&nbsp;name=<span class="string">&quot;Upload&quot;</span>&nbsp;&nbsp;&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;</li><li>Submit&nbsp;Query&nbsp;&nbsp;&nbsp;&nbsp;</li><li class="alt">--$&#123;bound&#125;--&nbsp;&nbsp;&nbsp;&nbsp;</li></ol></div><p style="margin-bottom: 0px; color: rgb(85, 85, 85); font-family: Arial; line-height: 26px;"><span style="color: rgb(0, 0, 0);">其中$&#123;bound&#125;为之前头信息中的分割符，如果头信息中规定为123，那么这里也要为123,；可以很容易看出，这个请求体是多个相同的部分组成的：每一个部分都是以--加分隔符开始的，然后是该部分内容的描述信息，然后一个回车，然后是描述信息的具体内容；如果传送的内容是一个文件的话，那么还会包含文件名信息，以及文件内容的类型。上面的第二个小部分其实是一个文件体的结构，最后会以--分割符--结尾，表示请求体结束。</span></p><p style="margin-bottom: 0px; font-family: Arial; line-height: 26px;">综上，可以知道要发送一个multipart/form-data的请求，其实任何支持post请求的工具或语言都可以支持，只是自己要稍微包装一下便可。</p><br/>Tags - <a href="http://pic1.liuxinxiu.com:80/tags/form-data/" rel="tag">form-data</a> , <a href="http://pic1.liuxinxiu.com:80/tags/form/" rel="tag">form</a> , <a href="http://pic1.liuxinxiu.com:80/tags/multipart/" rel="tag">multipart</a>
]]>
</description>
</item>
</channel>
</rss>