0

I am trying to resolve a SoundCloud URL to a track ID, so I later on can use the track ID with SC.stream().

This code works, but since it's async it doesn't work with the rest of my code:

$.getJSON(
  'http://api.soundcloud.com/resolve.json?url=' + trackUrl + '&client_id=' + clientId,
  function(result) {
    console.log('Track ID: ' + result.id);
    setTrackId(result.id);
  }
);

I'm trying to do the same thing with .ajax() so it can be async, but I can't get that to work. Console shows a GET error.

Here's the code:

$.ajax({
  url:      'http://api.soundcloud.com/resolve.json?url=' + trackUrl + '&client_id=' + clientId,
  dataType: 'json',
  async:    false,
  success:  function(result) {
              console.log('Track ID: ' + result.id);
              setTrackId(result.id);
            }
});

How can I get my ajax code to work? Or am I doing this all wrong? Tried to search for an answer but didn't find anything useful. Thanks.

EDIT: Here's the rest of my code: http://codepen.io/etzolin/pen/Jonqe

Etzolin
  • 33
  • 1
  • 1
  • 5
  • plz post code complete error which you are getting in console.. – Kartikeya Khosla Sep 06 '14 at 16:23
  • How is getJSON different in synchronicity, it's only a shortcut for $.ajax() (http://stackoverflow.com/questions/5922175/jquery-getjson-vs-ajax-json) – Benny Bottema Sep 06 '14 at 16:23
  • 1
    This is an X/Y problem, you're trying to make something synchronous, when you should be getting your other code to work with the asynchronous nature of ajax. – adeneo Sep 06 '14 at 16:28
  • 2
    1) Never use async false. 2) you are using a `success` callback anyway so your code *would* support async ajax. 3) Please show the rest of the related code :) – iCollect.it Ltd Sep 06 '14 at 16:29
  • The problem is that you are doing a cross domain call, so it won't work with a simple "get" ajax call. You should try jsonp. Anyway, crossdomain ajax calls with jquery doesn't support synchronous operations. Check the documentation of jQuery.ajax (http://api.jquery.com/jquery.ajax/). You have all the information there. – Mindastic Sep 06 '14 at 17:07
  • @TrueBlueAussie Here's my code: [link](http://codepen.io/etzolin/pen/Jonqe) – Etzolin Sep 06 '14 at 17:09
  • @Mindastic Thanks, that explains a lot. I've read the documentation page several times but somehow I missed it. I guess I should fix the rest of my code to work with this. – Etzolin Sep 06 '14 at 17:14

2 Answers2

0

Since I was doing a cross domain call I couldn't use async: false and it seems like I never should've done it anyway.

I solved my problem by using $.getJSON() and simply making my music stream code a function so it can be run at success callback:

$.getJSON(
  'http://api.soundcloud.com/resolve.json?url=' + trackUrl + '&client_id=' + clientId, function(result) {
    console.log('Track ID: ' + result.id);
    setTrackId(result.id);
    streamTrack();
  }
);
Etzolin
  • 33
  • 1
  • 1
  • 5
0

As per your answer since you are making a cross domain call, you can have an option set in the .ajax call for the same like this:

$.ajax({
  url:      'http://api.soundcloud.com/resolve.json?url=' + trackUrl + '&client_id=' + clientId,
  dataType: 'json',
  async:    false,
  crossDomain: true,
  success:  function(result) {
              console.log('Track ID: ' + result.id);
              setTrackId(result.id);
            }
});

Over here there is an option of setting crossDomain to true. It is available since version 1.5

V31
  • 7,626
  • 3
  • 26
  • 44