SoundCloud API gives “Uncaught SyntaxError: Unexpe

2019-05-10 08:49发布

问题:

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.

<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.

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

回答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.

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:

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

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


回答3:

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.



回答4:

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!