首页
导航
博客
电子书
算法
众创
代码
随贴
关于我们
您好,欢迎来到码863代码分享网! 请
[登录]
/
[注册]
搜 索
标题:
*
140
字
TAG标签:
(用空格隔开)
30
字
恢复历史版本:
请选择分类
html
python
javascript
php
sql
c
c++
c#
java
plain
所有人可见
仅自己可见
编辑器:UEditor
编辑器:TinyMCE
编辑器:Editor.md
HTML转MD
HTML转MD2
<p><a href="http://www.ruanyifeng.com/blog/2016/04/cors.html" _src="http://www.ruanyifeng.com/blog/2016/04/cors.html">http://www.ruanyifeng.com/blog/2016/04/cors.html</a></p><p><br/></p><p><br/></p><p>CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing)。</p><p>它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了AJAX只能同源使用的限制。</p><p>本文详细介绍CORS的内部机制。</p><p><br/></p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; ">一、简介</h2><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">CORS需要浏览器和服务器同时支持。目前,所有浏览器都支持该功能,IE浏览器不能低于IE10。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">整个CORS通信过程,都是浏览器自动完成,不需要用户参与。对于开发者来说,CORS通信与同源的AJAX通信没有差别,代码完全一样。浏览器一旦发现AJAX请求跨源,就会自动添加一些附加的头信息,有时还会多出一次附加的请求,但用户不会有感觉。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">因此,实现CORS通信的关键是服务器。只要服务器实现了CORS接口,就可以跨源通信。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; ">二、两种请求</h2><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">浏览器将CORS请求分成两类:简单请求(simple request)和非简单请求(not-so-simple request)。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">只要同时满足以下两大条件,就属于简单请求。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(1) 请求方法是以下三种方法之一:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>HEAD</p></li><li><p>GET</p></li><li><p>POST</p></li></ul><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(2)HTTP的头信息不超出以下几种字段:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>Accept</p></li><li><p>Accept-Language</p></li><li><p>Content-Language</p></li><li><p>Last-Event-ID</p></li><li><p>Content-Type:只限于三个值<code style="margin: 0px; padding: 0px; list-style-type: none; border: none;">application/x-www-form-urlencoded</code>、<code style="margin: 0px; padding: 0px; list-style-type: none; border: none;">multipart/form-data</code>、<code style="margin: 0px; padding: 0px; list-style-type: none; border: none;">text/plain</code></p></li></ul></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">凡是不同时满足上面两个条件,就属于非简单请求。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">浏览器对这两种请求的处理,是不一样的。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; ">三、简单请求</h2><h3 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 1.87em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; ">3.1 基本流程</h3><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">对于简单请求,浏览器直接发出CORS请求。具体来说,就是在头信息之中,增加一个<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>字段。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">下面是一个例子,浏览器发现这次跨源AJAX请求是简单请求,就自动在头信息之中,添加一个<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>字段。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">GET /cors HTTP/1.1Origin: http://api.bob.comHost: api.alice.comAccept-Language: en-USConnection: keep-aliveUser-Agent: Mozilla/5.0...</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">上面的头信息中,<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>字段用来说明,本次请求来自哪个源(协议 + 域名 + 端口)。服务器根据这个值,决定是否同意这次请求。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">如果<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>指定的源,不在许可范围内,服务器会返回一个正常的HTTP回应。浏览器发现,这个回应的头信息没有包含<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Allow-Origin</code>字段(详见下文),就知道出错了,从而抛出一个错误,被<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">XMLHttpRequest</code>的<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">onerror</code>回调函数捕获。注意,这种错误无法通过状态码识别,因为HTTP回应的状态码有可能是200。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">如果<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>指定的域名在许可范围内,服务器返回的响应,会多出几个头信息字段。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">Access-Control-Allow-Origin: http://api.bob.comAccess-Control-Allow-Credentials: trueAccess-Control-Expose-Headers: FooBarContent-Type: text/html; charset=utf-8</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">上面的头信息之中,有三个与CORS请求相关的字段,都以<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-</code>开头。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; "><span style="font-weight: 800;">(1)Access-Control-Allow-Origin</span></p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">该字段是必须的。它的值要么是请求时<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>字段的值,要么是一个<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">*</code>,表示接受任意域名的请求。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; "><span style="font-weight: 800;">(2)Access-Control-Allow-Credentials</span></p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">该字段可选。它的值是一个布尔值,表示是否允许发送Cookie。默认情况下,Cookie不包括在CORS请求之中。设为<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">true</code>,即表示服务器明确许可,Cookie可以包含在请求中,一起发给服务器。这个值也只能设为<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">true</code>,如果服务器不要浏览器发送Cookie,删除该字段即可。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; "><span style="font-weight: 800;">(3)Access-Control-Expose-Headers</span></p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">该字段可选。CORS请求时,<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">XMLHttpRequest</code>对象的<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">getResponseHeader()</code>方法只能拿到6个基本字段:<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Cache-Control</code>、<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Content-Language</code>、<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Content-Type</code>、<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Expires</code>、<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Last-Modified</code>、<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Pragma</code>。如果想拿到其他字段,就必须在<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Expose-Headers</code>里面指定。上面的例子指定,<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">getResponseHeader('FooBar')</code>可以返回<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">FooBar</code>字段的值。</p><h3 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 1.87em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; ">3.2 withCredentials 属性</h3><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">上面说到,CORS请求默认不发送Cookie和HTTP认证信息。如果要把Cookie发到服务器,一方面要服务器同意,指定<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Allow-Credentials</code>字段。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">Access-Control-Allow-Credentials: true</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">另一方面,开发者必须在AJAX请求中打开<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">withCredentials</code>属性。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-javascript" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">var xhr = new XMLHttpRequest();xhr.withCredentials = true;</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">否则,即使服务器同意发送Cookie,浏览器也不会发送。或者,服务器要求设置Cookie,浏览器也不会处理。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">但是,如果省略<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">withCredentials</code>设置,有的浏览器还是会一起发送Cookie。这时,可以显式关闭<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">withCredentials</code>。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-javascript" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">xhr.withCredentials = false;</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">需要注意的是,如果要发送Cookie,<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Allow-Origin</code>就不能设为星号,必须指定明确的、与请求网页一致的域名。同时,Cookie依然遵循同源政策,只有用服务器域名设置的Cookie才会上传,其他域名的Cookie并不会上传,且(跨源)原网页代码中的<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">document.cookie</code>也无法读取服务器域名下的Cookie。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; ">四、非简单请求</h2><h3 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 1.87em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; ">4.1 预检请求</h3><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">非简单请求是那种对服务器有特殊要求的请求,比如请求方法是<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">PUT</code>或<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">DELETE</code>,或者<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Content-Type</code>字段的类型是<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">application/json</code>。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">非简单请求的CORS请求,会在正式通信之前,增加一次HTTP查询请求,称为"预检"请求(preflight)。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">浏览器先询问服务器,当前网页所在的域名是否在服务器的许可名单之中,以及可以使用哪些HTTP动词和头信息字段。只有得到肯定答复,浏览器才会发出正式的<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">XMLHttpRequest</code>请求,否则就报错。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">下面是一段浏览器的JavaScript脚本。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-javascript" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">var url = 'http://api.alice.com/cors';var xhr = new XMLHttpRequest();xhr.open('PUT', url, true);xhr.setRequestHeader('X-Custom-Header', 'value');xhr.send();</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">上面代码中,HTTP请求的方法是<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">PUT</code>,并且发送一个自定义头信息<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">X-Custom-Header</code>。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">浏览器发现,这是一个非简单请求,就自动发出一个"预检"请求,要求服务器确认可以这样请求。下面是这个"预检"请求的HTTP头信息。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">OPTIONS /cors HTTP/1.1Origin: http://api.bob.comAccess-Control-Request-Method: PUTAccess-Control-Request-Headers: X-Custom-HeaderHost: api.alice.comAccept-Language: en-USConnection: keep-aliveUser-Agent: Mozilla/5.0...</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">"预检"请求用的请求方法是<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">OPTIONS</code>,表示这个请求是用来询问的。头信息里面,关键字段是<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>,表示请求来自哪个源。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">除了<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>字段,"预检"请求的头信息包括两个特殊字段。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; "><span style="font-weight: 800;">(1)Access-Control-Request-Method</span></p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">该字段是必须的,用来列出浏览器的CORS请求会用到哪些HTTP方法,上例是<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">PUT</code>。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; "><span style="font-weight: 800;">(2)Access-Control-Request-Headers</span></p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">该字段是一个逗号分隔的字符串,指定浏览器CORS请求会额外发送的头信息字段,上例是<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">X-Custom-Header</code>。</p><h3 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 1.87em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; ">4.2 预检请求的回应</h3><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">服务器收到"预检"请求以后,检查了<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>、<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Request-Method</code>和<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Request-Headers</code>字段以后,确认允许跨源请求,就可以做出回应。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">HTTP/1.1 200 OKDate: Mon, 01 Dec 2008 01:15:39 GMTServer: Apache/2.0.61 (Unix)Access-Control-Allow-Origin: http://api.bob.comAccess-Control-Allow-Methods: GET, POST, PUTAccess-Control-Allow-Headers: X-Custom-HeaderContent-Type: text/html; charset=utf-8Content-Encoding: gzipContent-Length: 0Keep-Alive: timeout=2, max=100Connection: Keep-AliveContent-Type: text/plain</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">上面的HTTP回应中,关键的是<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Allow-Origin</code>字段,表示<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">http://api.bob.com</code>可以请求数据。该字段也可以设为星号,表示同意任意跨源请求。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">Access-Control-Allow-Origin: *</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">如果浏览器否定了"预检"请求,会返回一个正常的HTTP回应,但是没有任何CORS相关的头信息字段。这时,浏览器就会认定,服务器不同意预检请求,因此触发一个错误,被<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">XMLHttpRequest</code>对象的<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">onerror</code>回调函数捕获。控制台会打印出如下的报错信息。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-bash" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">XMLHttpRequest cannot load http://api.alice.com.Origin http://api.bob.com is not allowed by Access-Control-Allow-Origin.</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">服务器回应的其他CORS相关字段如下。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">Access-Control-Allow-Methods: GET, POST, PUTAccess-Control-Allow-Headers: X-Custom-HeaderAccess-Control-Allow-Credentials: trueAccess-Control-Max-Age: 1728000</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; "><span style="font-weight: 800;">(1)Access-Control-Allow-Methods</span></p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">该字段必需,它的值是逗号分隔的一个字符串,表明服务器支持的所有跨域请求的方法。注意,返回的是所有支持的方法,而不单是浏览器请求的那个方法。这是为了避免多次"预检"请求。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; "><span style="font-weight: 800;">(2)Access-Control-Allow-Headers</span></p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">如果浏览器请求包括<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Request-Headers</code>字段,则<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Allow-Headers</code>字段是必需的。它也是一个逗号分隔的字符串,表明服务器支持的所有头信息字段,不限于浏览器在"预检"中请求的字段。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; "><span style="font-weight: 800;">(3)Access-Control-Allow-Credentials</span></p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">该字段与简单请求时的含义相同。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; "><span style="font-weight: 800;">(4)Access-Control-Max-Age</span></p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">该字段可选,用来指定本次预检请求的有效期,单位为秒。上面结果中,有效期是20天(1728000秒),即允许缓存该条回应1728000秒(即20天),在此期间,不用发出另一条预检请求。</p><h3 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 1.87em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; ">4.3 浏览器的正常请求和回应</h3><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">一旦服务器通过了"预检"请求,以后每次浏览器正常的CORS请求,就都跟简单请求一样,会有一个<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>头信息字段。服务器的回应,也都会有一个<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Allow-Origin</code>头信息字段。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">下面是"预检"请求之后,浏览器的正常CORS请求。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">PUT /cors HTTP/1.1Origin: http://api.bob.comHost: api.alice.comX-Custom-Header: valueAccept-Language: en-USConnection: keep-aliveUser-Agent: Mozilla/5.0...</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">上面头信息的<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Origin</code>字段是浏览器自动添加的。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">下面是服务器正常的回应。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 12px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", monospace; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">Access-Control-Allow-Origin: http://api.bob.comContent-Type: text/html; charset=utf-8</pre></blockquote><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">上面头信息中,<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">Access-Control-Allow-Origin</code>字段是每次回应都必定包含的。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; ">五、与JSONP的比较</h2><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">CORS与JSONP的使用目的相同,但是比JSONP更强大。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; color: rgb(17, 17, 17); font-size: 1.6em; line-height: 28.8px; font-family: Georgia, serif; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px; ">JSONP只支持<code style="margin: auto 3px; padding: 0px 5px; list-style-type: none; border: none; display: inline-block; font-size: 15.6px; background-color: pink; border-radius: 5px;">GET</code>请求,CORS支持所有类型的HTTP请求。JSONP的优势在于支持老式浏览器,以及可以向不支持CORS的网站请求数据。</p><p><br/></p>
CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网
湘ICP备2023018646号-1
MYSQl共执行 4 个查询,用时 0.0019738674163818 秒,PHP脚本用时 0.004895 秒,占用内存 0.851 MB,Gzip 已启用