VAST 的跨源资源共享 (CORS)
新型浏览器会对 JavaScript 网络请求应用同源安全限制,这意味着从一个来源运行的 Web 应用无法检索从其他来源提供的数据。对于 VAST,这种安全限制可防止
从 JavaScript VAST 呈现代码发出的 JavaScript XMLHttpRequests 读取
从其他来源投放的 VAST 广告响应。
此安全限制是为了防止问题
从另一个来源读取用户无需该来源即可登录的数据
获得用户许可。该限制给在 JavaScript 中投放的 VAST 带来了问题
因为广告服务器通常与
。不过,W3C 建议使用跨源资源共享 (CORS) 标头来规避此限制,因为该标头允许跨不同源共享资源。
CORS 标头
为避免跨源问题,VAST 广告服务器对 SDK 发出的请求的响应必须包含以下 HTTP CORS 标头:
Access-Control-Allow-Origin: <origin header value>
Access-Control-Allow-Credentials: true
通过这些标头,任何来源的广告播放器都可以从广告服务器来源读取 VAST 响应。将 Access-Control-Allow-Origin
的值设置为随广告请求一起发送的 Origin
标头的值,并将 Access-Control-Allow-Credentials
设置为 true
,以确保正确发送和接收 Cookie。
如需详细了解如何启用 CORS,请参阅
启用跨源资源共享。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-07。
[null,null,["最后更新时间 (UTC):2025-03-07。"],[[["Modern browsers restrict JavaScript from accessing data from different origins for security reasons, impacting VAST ads served from a separate domain than the player."],["Cross-Origin Resource Sharing (CORS) headers enable cross-origin data sharing, allowing VAST ads to be served from a different domain than the player."],["VAST ad server responses should include specific CORS headers: `Access-Control-Allow-Origin` (set to the request's `Origin` header value) and `Access-Control-Allow-Credentials` (set to `true`)."]]],[]]