[英]XMLHTTPRequest Error: Synchronous XMLHttpRequest on the main thread is deprecated … (SoundCloud API)
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= ). 我正在使用XMLHttpRequest访问SoundClouds热门趋势跟踪( 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 . 我花了一些时间寻找答案, 据我所知,问题是请求中包含一些javascript 。 So I looked into the SoundCloud API and found this little bit in every track's properties:
因此,我研究了SoundCloud API,并在每个音轨的属性中发现了这一点:
... "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. 因此,我发现的针对类似问题的所有解决方案均无法使用,因为我无法更改其API的工作方式。 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. 如果发生任何更改,我将其称为“ DOMContentLoaded”。
Maybe you can help me. 也许你可以帮我。 Thanks.
谢谢。
You are using XMLHttpRequest wrong. 您使用XMLHttpRequest错误。 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. 但是即使那样,您仍将
No 'Access-Control-Allow-Origin' header is present on the requested resource
错误中No 'Access-Control-Allow-Origin' header is present on the requested resource
。 Which is CORS browser policy error. 这是CORS浏览器策略错误。 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. 因此,如果可以修改服务器代码,请从服务器执行该请求,然后更改客户端AJAX请求以从服务器请求数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.