在控制台中,它给我错误“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&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/