Deprecation Synchronous XMLHttpRequest

。_饼干妹妹 提交于 2021-01-05 07:02:55

问题


I see this error: "[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.", on this code:

var request;
if(window.XMLHttpRequest){
    request = new XMLHttpRequest();
}else{
    request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open('GET', 'http://www.mywebsite.com', false);
request.send();

What should I replace the instructions with because they are deprecated?


回答1:


you just need to change

request.open('GET', 'http://www.mywebsite.com', false);

to

request.open('GET', 'http://www.mywebsite.com', true);



回答2:


request.open('GET', 'http://www.mywebsite.com', false)

It means you are creating a synchronous call to the Web service. So not expecting any call back theoratically.

So to continue with above approach make sure the body is also null.

request.send(null);

if (request.status === 200) {
  console.log(request.responseText);
}

The null parameter indicates that no body content is needed for the GET request. Hence the thread won't be blocked.



来源:https://stackoverflow.com/questions/48257085/deprecation-synchronous-xmlhttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!