SoundCloud API gives “Uncaught SyntaxError: Unexpected token : ” error

帅比萌擦擦* 提交于 2019-12-05 22:05:23

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.

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

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

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

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

A way to "get" it is using an asynchronous ajax request like $.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.

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!