小编Sco*_*son的帖子

如何在 iframe 中允许 Geolocation API?

我有一个 iframe 标签,其中 src 是不同服务器上的另一个网页。我有能力修改两个站点的标题。在开始实施控制安全策略之前,我能够单击 iframe 内的按钮并检索 GPS 坐标。我相信控制安全策略会阻止我的父站点运行 Geolocation API。

父站点的代码:

<customHeaders>
    <add name="Content-Security-Policy" value="frame-src 'self' https://MyChildSite.com" />
</customHeaders>
Run Code Online (Sandbox Code Playgroud)
<html>
    <iframe src="https://MyChildSite.com" allow="geolocation"></iframe>
</html>
Run Code Online (Sandbox Code Playgroud)

子站点的代码:

<customHeaders>
      <add name="Content-Security-Policy" value="frame-src 'self' https://MyParentSite.com" />
      <add name="Feature-Policy" value="geolocation 'self' https://MyParentSite.com" />
</customHeaders>
Run Code Online (Sandbox Code Playgroud)
<html>
    <button onclick="getCoordinates()">Get GPS</button>
    ...list some stuff
</html>
Run Code Online (Sandbox Code Playgroud)

当我通过父站点单击子站点上的按钮时,我没有从坐标中得到我期望的响应。这个问题有方法解决吗?

iframe geolocation content-security-policy feature-policy

2
推荐指数
1
解决办法
1477
查看次数