brighthero
brighthero

Reputation: 135

Why do I get Uncaught SyntaxError: Unexpected token ILLEGAL

I want to use AJAX and the SoundCloud API to get the tracks of a user.

jQuery.ajax({
              url: 'http://api.soundcloud.com/resolve?url=http://soundcloud.com/[SOME USER]/tracks/&format=json&consumer_key=[MY KEY]&callback=?'
            , dataType: 'jsonp'
            , success: function( data ) {
                console.log( data );
            }
            });

I can see that chrome gets the json data but I get the error

Uncaught SyntaxError: Unexpected token ILLEGAL

in the console.

The issue is probably that the json file is an json array. Could that be the error? And if yes, how can I convert the array into single objects?

Upvotes: 2

Views: 3737

Answers (1)

apsillers
apsillers

Reputation: 115980

The problem is that the description field in track id #159500192 ('Summer Chords Pt. 2' (Electro House Mix)) has invisible characters that are not legal inside of JavaScript strings, so the JavaScript parser chokes when trying to the run the JSONP response as a script. SoundCloud should encode these values when serving content via JSONP.

Because SoundCloud supports CORS, you don't need to use JSONP at all. You can simply request the file directly, by removing the callback=? parameter and using dataType: json (not jsonp):

jQuery.ajax({ url: "https://api.soundcloud.com/resolve?url=http://soundcloud.com/[USER]/tracks/&format=json&consumer_key=[KEY]",
              dataType: 'json',
              success: function(d) { console.log(d); }
});

Upvotes: 4

Related Questions