gpt4 book ai didi

javascript - 尝试访问 JavaScript 中的 iframe 时出错

转载 作者:数据小太阳 更新时间:2023-10-29 05:55:35 29 4
gpt4 key购买 nike

我正在尝试从这个小 API 调用函数 SC.Widget:http://developers.soundcloud.com/docs/api/html5-widget ,但我在 Chrome 检查器中收到此错误消息,我被困在那里。

Unsafe JavaScript attempt to access frame with URL file://localhost/Users/maxwell/Desktop/test/test.html from frame withURL

http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F67825032&auto_play=false&show_artwork=true&color=ff7700.

The frame requesting access has a protocol of 'http', the frame being accessed has a protocol of 'file'. Protocols must match.

<body>
<iframe id="soundcloud" width="100%" height="166" scrolling="no" frameborder="no"
src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F67825032&amp;auto_play=false&amp;show_artwork=true&amp;color=ff7700"></iframe>
<script>
Soundcloud();
</script>
</body>


function Soundcloud() {
var widget1 = SC.Widget(iframeElement.soundcloud);
alert("widget1");
}

我知道这样做是出于安全原因,但如果我无法访问框架,该如何修改 SoundCloud 小部件?

谢谢你的帮助!

最佳答案

当使用 JavaScript 访问 iframe 或发出任何 JSON AJAX 请求时,您只能在它们位于同一域时获得访问权限或响应。否则,服务器必须显式设置:

访问控制允许来源:*

在标题中。您还可以提供以逗号分隔的允许域列表,而不是 *。

所以你必须在网页和数据源在同一个域的开发环境中测试这个,比如localhost,否则你真的没有什么可以做的,除非你用--disable启动chrome -网络安全

关于javascript - 尝试访问 JavaScript 中的 iframe 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13446106/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com