最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - XMLHTTPRequest Error: Synchronous XMLHttpRequest on the main thread is deprecated ... (SoundCloud API) - Stack Over

programmeradmin7浏览0评论

I'm using an XMLHttpRequest to access SoundClouds Top Trending Tracks (;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 
/

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":".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 pletely wrong and you guys can help.

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

function initialSearch() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', ";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.

I'm using an XMLHttpRequest to access SoundClouds Top Trending Tracks (https://api-v2.soundcloud./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/

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./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 pletely wrong and you guys can help.

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

function initialSearch() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', "https://api-v2.soundcloud./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.

Share Improve this question asked Mar 30, 2018 at 15:45 etlaMetlaM 1401 gold badge1 silver badge12 bronze badges 2
  • 1 i mean... that ajax request is synchronous. make it asynchronous by changing false to true. – Kevin B Commented Mar 30, 2018 at 15:47
  • For help with the problems this change will cause, see this question: stackoverflow./questions/14220321/… – Kevin B Commented Mar 30, 2018 at 15:50
Add a ment  | 

1 Answer 1

Reset to default 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./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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论