Javascript SDK connect()函数在chrome中不起作用

时间:2012-03-08 04:57:17

标签: soundcloud

我尝试在此示例之后对代码进行建模:http://connect.soundcloud.com/examples/connecting.html#

它适用于firefox,但不适用于chrome。在chrome中,soundcloud弹出窗口正确显示,我可以登录(返回sc-connect.html),但窗口不会关闭。经过仔细检查,有一个javascript错误,因为window.opener为null。我想知道它是否与localhost uri有关?上面链接中的示例适用于firefox和chrome。有任何想法吗?我的代码如下:

SC.initialize({client_id:'my_client_id', redirect_uri:'http://localhost:3000/sc-connect.html'});

$('button').click(function(){
  SC.connect(function () {
    console.log('made it');
  }
}

我的sc-connect.html页面如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html>
    <head>
      <title>Connect with SoundCloud</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body onload="window.opener.setTimeout(window.opener.SC.connectCallback, 1)">
      <b style="width: 100%; text-align: center;">This popup should automatically close in a few seconds</b>
    </body>
  </html>

在此应用的soundcloud上重定向URI:http://localhost:3000/sc-connect.html

3 个答案:

答案 0 :(得分:4)

这实际上是Chrome中的一个奇怪错误,这是由Chrome App Store安装SoundCloud应用程序引起的。奇怪,我知道。

解决方法是使用window.opener而不是使用{{1}},将oauth令牌推送到LocalStorage或SessionStorage,并让开启窗口监听Storage event

答案 1 :(得分:0)

您的示例代码中似乎缺少几个右括号。除此之外,你的例子似乎没有任何明显的错误。我复制并粘贴了你的sc-connect.html并将其与以下内容一起使用:

<html>
<head>
  <title>Demo</title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
  <script type="text/javascript" src="http://connect.soundcloud.com/sdk.js"></script>
  <script type="text/javascript">

  $(document).ready(function() {
      SC.initialize({
          client_id: 'MY_CLIENT_ID',
          redirect_uri: 'http://localhost:8080/connect/sc-callback.html'
      });

      $('button').click(function(){
          SC.connect(function() {
              SC.get('/me', function(data) {
                  $('#name').text(data.username);
              });
          });
      });
  });

  </script>
</head>
<body>
  <button>Connect</button>
  <p>
    Hello There, <span id="name"></span>
  </p>
</body>
</html>

这适用于Firefox和Chrome。如果有帮助,请告诉我。

答案 2 :(得分:0)

    <!DOCTYPE html> <html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Connect with SoundCloud</title> 

</head>
<body onload="window.opener.setTimeout(window.opener.SC.connectCallback, 1)">
    <b style="text-align: center;">This popup should automatically close in a few seconds</b>
    <script type="text/javascript">window.opener.SC.connectCallback.call(this); </script> 
</body> 
</html> 
相关问题