阅读背景:

SoundCloud API提供“Uncaught SyntaxError:Unexpected token:”错误

来源:互联网 

In the console it's giving me the error "Uncaught SyntaxError: Unexpected token : ", but if I access direct SoundCloud URL in my browser then it's giving valid JSON. Earlier this code was working fine and today this issue started.

在控制台中,它给出了错误“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>

Update:

Below is the actual code for which I am asking the question, above html I just created for example.

下面是我问的问题的实际代码,例如我刚刚创建的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);
};

4 个解决方案

#1


1  

I got the reason of issue, earlier soundcloud were responding response in jsonp but now they are providing JSON even I passed JsonP callback function. I had to make ajax request to fix it.

我得到了问题的原因,早期的soundcloud在jsonp中响应响应,但现在他们提供JSON,即使我传递了JsonP回调函数。我不得不提出修复它的ajax请求。

I used following code to fix it.

我使用以下代码来修复它。

    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();

#2


0  

The following script tag expects JavaScript code in the source and not JSON.

以下脚本标记需要源代码中的JavaScript代码而不是JSON。

<script src="file.js"></script> 

#3


0  

I suppose that you want to use this externally produced json...

我想你想用这个外部生产的json ......

A way to "get" it is using an asynchronous ajax request like $.get(url,callback);

“获取”它的方法是使用异步ajax请求,如$ .get(url,callback);

Calling it as a script will sure fail...
Because it's not a script.

将其称为脚本肯定会失败...因为它不是脚本。

Try to run the snippet!

尝试运行代码段!

var url = "https://api.soundcloud.com/resolve.json?url=https://api.soundcloud.com/tracks/251912676/?secret_token=s-EkyTy&amp;client_id=08f79801a998c381762ec5b15e4914d5"

var json;

$.get(url,function(result){
    json = result;

    // show in console
    console.log(JSON.stringify(json));
    
    // Now using it...
    $("#json_usage").html(json.tag_list+" and all the "+json.permalink);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<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 <span id="json_usage"></span> !</h2>
    </body>
</html>

In the above, the resulting json is placed in the json variable and then console logged.

在上面,生成的json放在json变量中,然后控制台记录。

#4


0  

Sorry you've been having trouble with JSONP responses from the SoundCloud API. This was due to a bug that made it into production in the last few days. We've just deployed a fix, and so this endpoint will now be returning valid JSONP responses rather than just JSON, if you specify a callback parameter. Sorry for the confusion!

抱歉,您在使用SoundCloud API时遇到JSONP响应问题。这是由于一个错误导致它在过去几天投入生产。我们刚刚部署了一个修复程序,因此如果指定了一个回调参数,此端点现在将返回有效的JSONP响应,而不仅仅是JSON。对困惑感到抱歉!


分享到: