尝试在JavaScript中访问iframe时出错

5 javascript soundcloud

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

不安全的JavaScript尝试使用URL file://localhost/Users/maxwell/Desktop/test/test.htmlwithURL 访问带有URL 的帧

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

请求访问的帧具有"http"协议,被访问的帧具有"文件"协议.协议必须匹配.

<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");
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮忙!

red*_*ine 8

使用JavaScript访问iframe或发出任何JSON AJAX请求时,只有当它们位于同一个域时才能获得访问权限或响应.否则,服务器必须明确设置:

Access-Control-Allow-Origin:*

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

因此,您必须在网页和数据源位于同一域的开发环境中进行测试,例如localhost,否则除非您使用--disable-web-启动chrome,否则您无法做任何其他事情.安全