两个JSFiddles具有完全相同的代码,但一个起作用而另一个不起作用

时间:2020-05-30 16:13:23

标签: javascript html jquery api

以下SJFiddle工作正常。

http://jsfiddle.net/JMPerez/62wafrm7/

我创建了第二个SJFiddle进行测试,并复制了与第一个完全相同的所有内容(HTML / CSS / JS)。我还提供了相同的资源,但是不起作用。

https://jsfiddle.net/kennethcode/dz1tubf6/

HTML两者相同

<div class="container">
    <h1>Displaying User Data</h1>
    <p>Log in with your Spotify account and this demo will display information about you fetched using the Spotify Web API</p>
    <button class="btn btn-primary" id="btn-login">Login</button>
    <div id="result"></div>
</div>
<script id="result-template" type="text/x-handlebars-template">
    <dl>
      <img src="{{images.0.url}}">
      <dt>User Name</dt>
      <dd>{{id}}</dd>
      <dt>Display Name</dt>
      <dd>{{display_name}}</dd>
      <dt>Country</dt>
      <dd>{{country}}</dd>
      <dt>Followers</dt>
      <dd>{{followers.total}}</dd>
      <dt>Profile</dt>
      <dd><a href="{{external_urls.spotify}}" target="_blank">{{external_urls.spotify}}</a></dd>
      <dt>Email</dt>
      <dd>{{email}}</dd>
      <dt>Product</dt>
      <dd>{{product}}</dd>
    </dl>
</script>

JavaScript两者相同

(function() {

    function login(callback) {
        var CLIENT_ID = '6b284830006843e7ae7b170725715aed';
        var REDIRECT_URI = 'http://jmperezperez.com/spotify-oauth-jsfiddle-proxy/';
        function getLoginURL(scopes) {
            return 'https://accounts.spotify.com/authorize?client_id=' + CLIENT_ID +
              '&redirect_uri=' + encodeURIComponent(REDIRECT_URI) +
              '&scope=' + encodeURIComponent(scopes.join(' ')) +
              '&response_type=token';
        }

        var url = getLoginURL([
            'user-read-email'
        ]);

        var width = 450,
            height = 730,
            left = (screen.width / 2) - (width / 2),
            top = (screen.height / 2) - (height / 2);

        window.addEventListener("message", function(event) {
            var hash = JSON.parse(event.data);
            if (hash.type == 'access_token') {
                callback(hash.access_token);
            }
        }, false);

        var w = window.open(url,
                            'Spotify',
                            'menubar=no,location=no,resizable=no,scrollbars=no,status=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left
                           );

    }

    function getUserData(accessToken) {
        return $.ajax({
            url: 'https://api.spotify.com/v1/me',
            headers: {
               'Authorization': 'Bearer ' + accessToken
            }
        });
    }

    var templateSource = document.getElementById('result-template').innerHTML,
        template = Handlebars.compile(templateSource),
        resultsPlaceholder = document.getElementById('result'),
        loginButton = document.getElementById('btn-login');

    loginButton.addEventListener('click', function() {
        login(function(accessToken) {
            getUserData(accessToken)
                .then(function(response) {
                    loginButton.style.display = 'none';
                    resultsPlaceholder.innerHTML = template(response);
                });
            });
    });

})();

两者的CSS相同

.container {
    margin: 1em;
}

img {
    margin-bottom: 1em;
}

任何线索为什么会发生这种情况?谢谢

2 个答案:

答案 0 :(得分:0)

检查侧栏中的“资源”部分。第一个拥有这3个额外资源:

  • cached.css
  • handlebars.min.js
  • jquery-1.10.1.min.js

答案 1 :(得分:0)

只需在 html部分顶部中手动添加所需的脚本:

  <link rel="stylesheet" type="text/css" href="https://developer.spotify.com/web-api/static/css/cached.css">
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0-alpha.1/handlebars.min.js"></script>
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.1.min.js"></script>