关于 javascript:XMLHTTPRequest Error: Synchronous XMLHttpRequest on the main thread is deprecated … (SoundCloud API)

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

我正在使用 XMLHttpRequest 访问 SoundClouds 热门歌曲 (https://api-v2.soundcloud.com/charts?kind=trending

  • 我的意思是... ajax 请求是同步的。通过将 false 更改为 true 使其异步。
  • 有关此更改将导致的问题的帮助,请参阅以下问题:stackoverflow.com/questions/14220321/...


您使用 XMLHttpRequest 错误。做异步请求而不是同步。这是一个固定版本:

1
2
3
4
5
6
7
8
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();
}

但即便如此,你也会得到 No 'Access-Control-Allow-Origin' header is present on the requested resource 错误。这是 CORS 浏览器策略错误。您只能向相同的源资源发出请求。

因此,如果您可以修改服务器代码,请从您的服务器执行该请求并更改您的客户端 AJAX 请求以从您的服务器请求数据。

  • 感谢您的回答。我已经改变了我的功能,现在正在做一个回调等等。我仍然收到 CORS 浏览器策略错误。如您所述,我将如何更改请求?对不起,打扰你 ...
  • 一点都不打扰。我知道如何做到这一点的唯一方法是在您的服务器上实现 soundcloud 请求作为端点。然后从你的 JS 你应该调用那个服务器端点。假设您在 http://localhost:5000/index 上托管网站,那么您的端点将类似于 http://localhost:5000/api/gettracks。希望能给出这个想法。如果您评价我的回答,也将不胜感激。
  • addEventListener 是否优于 onreadystatechange
  • 这是回复:stackoverflow.com/questions/14946291/...