简体   繁体   English

Access-Control-Allow-Origin不允许使用null

[英]Origin null is not allowed by Access-Control-Allow-Origin

I'm currently working with the SoundCloud API and would like to have a track embed when a button is clicked. 我目前正在使用SoundCloud API,并希望在单击按钮时嵌入轨道。

I get two errors: 我收到两个错误:

XMLHttpRequest cannot load http://soundcloud.com/oembed.json?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F48419073 . XMLHttpRequest无法加载http://soundcloud.com/oembed.json?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F48419073 Origin null is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-Origin不允许使用null。

AND

Uncaught TypeError: Cannot read property 'html' of null 未捕获的TypeError:无法读取null的属性'html'

Here is my code: 这是我的代码:

<button onclick="getPopular()">+1</button>
<div id="track"></div>

<script src="http://connect.soundcloud.com/sdk.js" type="text/JavaScript"></script>
<script type="text/JavaScript">
    SC.initialize({
        client_id: "**************",
    });

    var getPopular = function() {
        SC.get("/tracks", {limit: 1}, function(tracks) {
            var track = tracks[0];
            alert("Latest track: " + track.title);
            SC.oEmbed(track.uri, document.getElementById("track"));
            });
        };
</script>

I use an alert in my code to let me know that it is actually taking information from the SoundCloud API. 我在代码中使用警报让我知道它实际上是从SoundCloud API获取信息。 I'm just not sure what else is preventing it from embedding. 我只是不确定还有什么阻止它嵌入。

Thanks, ahead of time, or looking at my question. 提前谢谢,或者看看我的问题。

  • jiggabits jiggabits

Read a little about Same Origin Policy to understand your main problem better. 阅读一些有关同源策略的内容,以更好地了解您的主要问题。 Ajax, localhost and Chrome/Opera don't go well together. Ajax,localhost和Chrome / Opera不能很好地融合在一起。 This related question is even better. 这个相关的问题甚至更好。

Your second problem is due to the Ajax call (somewhere in your API) which doesn't return an html response due to the first error. 第二个问题是由于Ajax调用(在API中的某个地方),由于第一个错误而没有返回html响应。

Instead of explaining the issue (which is very well explained in the links above), I'll provide a solution. 我没有解释问题(在上面的链接中有很好的解释),而是提供解决方案。 Since you're running on Chrome, there's an workaround for that. 由于您在Chrome上运行,因此有一种解决方法。 Start chrome with this option: 使用此选项启动chrome:

--allow-file-access-from-files --allow-文件访问从 - 文件

(workaround which I shamelessly borrowed from Pointy ) (我从Pointy无耻地借来的解决方法)

You could also try running it on Firefox, or hosting it temporarily. 您也可以尝试在Firefox上运行它,或暂时托管它。 :) :)


PS If you plan on doing serious development from your local machine, you may consider installing Apache to serve and test content through http://localhost , thus lifting the file:/// restrictions. PS如果您计划从本地计算机进行认真开发,可以考虑安装Apache以通过http://localhost提供服务和测试内容,从而解除file:///限制。

Here are some excellent tools that come with Apache and PHP pre-configured for development: 以下是Apache和PHP预先配置用于开发的一些优秀工具:

If you're getting a track back, the I would try, alert(document.getElementById("track")); 如果您正在回溯,我会尝试,提醒(document.getElementById(“track”)); to make sure that you're getting your dom element. 确保你得到你的dom元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM