javascript - SoundCloud API 给出 ​​ "Uncaught SyntaxError: Unexpected token : "错误

标签 javascript jquery html jsonp soundcloud

在控制台中,它给我错误“Uncaught SyntaxError: Unexpected token :”,但如果我在浏览器中直接访问 SoundCloud URL,那么它会提供有效的 JSON。早些时候这段代码工作正常,今天开始出现这个问题。

<html>
    <head>
    <script src="https://api.soundcloud.com/resolve.json?url=https://api.soundcloud.com/tracks/251912676/?secret_token=s-EkyTy&amp;client_id=08f79801a998c381762ec5b15e4914d5"></script>
    </head>
    <body>
    <h2>hellooo</h2>
    </body>
</html>

更新:

下面是我提出问题的实际代码,例如我刚刚创建的 html。

SoundCloud.prototype._jsonp = function (url, callback) {
        var target = document.getElementsByTagName('script')[0] || document.head;
        var script = document.createElement('script');

        var id = 'jsonp_callback_' + Math.round(100000 * Math.random());
        window[id] = function (data) {
            if (script.parentNode) {
                script.parentNode.removeChild(script);
            }
            window[id] = function () {};
            callback(data);
        };

        script.src = url + (url.indexOf('?') >= 0 ? '&' : '?') + 'callback=' + id;
        target.parentNode.insertBefore(script, target);
};

最佳答案

我明白了问题的原因,之前的 soundcloud 是在 jsonp 中响应响应,但现在即使我通过了 JsonP 回调函数,他们也提供了 JSON。我不得不发出 ajax 请求来修复它。

我用下面的代码来修复它。

    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
           callback( JSON.parse(this.responseText) );
        }
    };
    xhttp.open("GET", url, true);
    xhttp.send();

关于javascript - SoundCloud API 给出 ​​ "Uncaught SyntaxError: Unexpected token : "错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41194022/

相关文章:

JavaScript 调用不工作

javascript - 如何使用 jspdf.js 正确打印 HTML 表格和 PDF?

javascript - 自动更改可点击幻灯片顶部的 div 的文本颜色

jquery - Squarespace 代码块对齐问题

javascript - jQuery 面板 slider 通过单击按钮打开但不会关闭

javascript - 如果我在两列中进行编辑,图像应该保持不变

javascript - 如何在没有 JavaScript 的情况下强制 HTML 元素保持恒定的宽高比?

javascript - win.toggleTabBar() 在 Electron 中不起作用

javascript - jquery promise 不会触发

javascript - 如何访问 JSON.parse 创建的 JSON 对象的元素?