【问题标题】:Javascript SDK connect() function not working in chromeJavascript SDK connect()函数在chrome中不起作用
【发布时间】:2012-03-08 04:57:17
【问题描述】:

我正在尝试在此处的示例之后为我的代码建模:http://connect.soundcloud.com/examples/connecting.html#

它适用于 Firefox,但不适用于 chrome。在 chrome 中,soundcloud 弹出窗口正确显示,我可以登录(返回 sc-connect.html),但随后窗口不会关闭。仔细检查后,由于 window.opener 为空,因此存在 javascript 错误。我想知道它是否与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

【问题讨论】:

  • 好的,所以经过更多测试后,上面的代码(在修复缺少的括号后)在 firefox、safari 和 chromium 中有效,但在 chrome 中无效(即使在没有扩展的 icognito 中)......不是确定有什么不同,特别是。在铬和铬之间

标签: soundcloud


【解决方案1】:

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

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

【讨论】:

  • 哇——这绝对是问题所在...卸载了 soundcloud 作为 chrome 上的应用程序,它现在可以工作了!非常感谢您验证我看到的问题!目前正在实施本地存储解决方案 - 感谢您的建议
  • 嗨杰夫思克。你有没有运气实施解决方案? JS 不是我的强项,我想知道您(或其他任何人)是否可以详细说明建议的解决方案。谢谢!
  • @afxjzs 如果您在使用本地存储或存储事件时遇到特定问题,您可以提出自己的问题!
  • 我对两者的经验都非常有限,所以我没有一个具体的问题。我只是希望/要求jeffthink 在他找到后发布他的解决方案。如果我自己解决了它仍然没有回答,我会在这里发布。这是 Chrome 错误,而不是 Soundcloud 错误,对吗?
  • @afxjzs 是的,这是 Chrome 的一个问题,我们已经尝试向他们的开发人员提出几次,但仍然没有得到任何回报......
【解决方案2】:

您的示例代码中似乎缺少几个右括号。除此之外,您的示例似乎没有明显错误。我复制并粘贴了您的 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。让我知道这是否有帮助。

【讨论】:

  • 嘿保罗 - 感谢您的回复,并很抱歉缺少括号(从咖啡脚本翻译)...请参阅我上面关于它在基本上所有其他浏览器中工作的评论...不确定是什么(设置?)寻找改变铬试图让它工作......有什么想法吗?同时,将只使用 firefox
【解决方案3】:
    <!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> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2013-08-04
    相关资源
    最近更新 更多