本文,由Panayiotis“ PVGR” Velisarakos进行了同行评审(感谢所有SitePoint Pose Peer Reviewer!),探索了交叉孔源资源共享(CORS),HTML5 API启用网站可以访问先前受限制的外部资源。 CORS放宽了同一原始政策,允许向不同域的请求。 例如,在CORS之前,跨域AJAX请求是不可能的。本文演示了CORS如何增强Web体验。
钥匙要点:
Access-Control-Allow-Origin
标题控制cookie访问。Access-Control-Allow-Credentials
<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173984767246953.jpg" class="lazy" https: alt="”" cors><code>crossorigin
>
anonymous
use-credentials
preflights:<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173984767246953.jpg" class="lazy" alt="An In-depth Look at CORS ">
<p><strong>Preflights:</strong></p>
<p>For complex requests (methods beyond GET/HEAD/POST, or custom headers), preflights (an initial OPTIONS request) verify server acceptance. The server responds with <code>Access-Control-Allow-Origin
对于复杂的请求(超出get/head/post或自定义标头)的复杂请求(初始选项请求)验证服务器接受。 服务器响应Access-Control-Allow-Credentials
,Access-Control-Allow-Methods
,Access-Control-Allow-Headers
,Access-Control-Max-Age
和Access-Control-Request-Method
>标题。客户端在前飞行中发送Access-Control-Request-Headers
>>>>
CORS和CANVAS IMAGES:>
要在画布中使用外部图像,服务器必须启用CORS(例如,通过在服务器配置中设置)。客户端代码需要Access-Control-Allow-Origin
。 没有CORS,就会发生安全例外。img.setAttribute('crossOrigin', 'anonymous');
>
>属性:crossorigin
>
header的cors请求。 crossorigin
省略凭据; Origin
包括它们(需要服务器端anonymous
)。use-credentials
>
Access-Control-Allow-Credentials: true
通过促进交叉原始资源访问,CORS可显着增强Web开发。 了解其安全含义并正确实施至关重要。
>
常见问题(常见问题解答):>
>常见问题解答部分提供了有关CORS的常见问题的详细答案,涵盖了标头的目的,cookie处理的目的,简单与预覆盖的请求,服务器端配置,安全风险,HTTP请求兼容性,特定标头,浏览器行为的作用,测试以及CORS和JSONP之间的差异。 (原始常见问题解答部分保留了整体。)以上是深入了解CORS的详细内容。更多信息请关注PHP中文网其他相关文章!