2

I'm using an XMLHttpRequest to access SoundClouds Top Trending Tracks (https://api-v2.soundcloud.com/charts?kind=trending&genre=soundcloud:genres:all-music&client_id=1dff55bf515582dc759594dac5ba46e9&q=). I can't even get to parsing and all the fun stuff with it, because my console logs this error:

Synchronous XMLHttpRequest on the main thread is deprecated because of 
its detrimental effects to the end user's experience. For more help 
http://xhr.spec.whatwg.org/

I spend some time looking for an answer and - as far as I understand - the problem is some javascript within an request. So I looked into the SoundCloud API and found this little bit in every track's properties:

... "waveform_url":"https://wis.sndcdn.com/2AVcYzUmENai_m.json" ...

So all solutions I've found to similar problems wouldn't work, as I can't change how their API works. Maybe I'm doing something completely wrong and you guys can help.

Here is the complete function which causes the problem (I think):

function initialSearch() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', "https://api-v2.soundcloud.com/charts?kind=trending&genre=soundcloud:genres:all-music&client_id=1dff55bf515582dc759594dac5ba46e9&q=", false);
    xhr.addEventListener("load", function() {
        initialArray = JSON.parse(xhr.response);
        }, false);
} 

I call it on "DOMContentLoaded" if it changes anything.

Maybe you can help me. Thanks.

etlaM
  • 131
  • 1
  • 1
  • 9
  • 1
    i mean... that ajax request is synchronous. make it asynchronous by changing false to true. – Kevin B Mar 30 '18 at 15:47
  • For help with the problems this change will cause, see this question: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Kevin B Mar 30 '18 at 15:50

1 Answers1

3

You are using XMLHttpRequest wrong. Do asynchronous request instead of synchronous. Here is a fixed version:

function initialSearch() {
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", function() {
        initialArray = JSON.parse(xhr.response);
    }, false);
    xhr.open('GET', "https://api-v2.soundcloud.com/charts?kind=trending&genre=soundcloud:genres:all-music&client_id=1dff55bf515582dc759594dac5ba46e9&q=");
    xhr.send();
}

But even then, you will get No 'Access-Control-Allow-Origin' header is present on the requested resource error. Which is CORS browser policy error. You can only make requests to the same origin resources.

So if you can modify your server code, do that request from your server and change your client AJAX request to ask data from your server.

andnik
  • 2,405
  • 2
  • 22
  • 33
  • Thanks for your answer. I've changed my function and am now doing a callback and all that. Still I get the CORS browser policy error. How would I change the request as you mentioned? Sorry for bothering you ... – etlaM Mar 31 '18 at 17:03
  • Not bothering at all. The only way I know how to do that is to implement soundcloud request on your server as an endpoint. Then from your JS you should call that server endpoint. Let's say if you host website on `http://localhost:5000/index` then your endpoint would be something like `http://localhost:5000/api/gettracks`. Hope that gives the idea. Also will appreciate if you rate my answer. – andnik Apr 01 '18 at 11:14
  • Is `addEventListener` better than `onreadystatechange`? – James Douglas Jan 18 '21 at 15:21
  • Here is the response: https://stackoverflow.com/questions/14946291/can-one-replace-xhr-onreadystatechange-with-xhr-onload-for-ajax-calls – andnik Jan 18 '21 at 15:33