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

javascript - Deprecation Synchronous XMLHttpRequest - Stack Overflow

programmeradmin3浏览0评论

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 /.", on this code:

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

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

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/.", on this code:

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

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

Share Improve this question asked Jan 15, 2018 at 4:49 HelpmeHelpme 1332 gold badges4 silver badges12 bronze badges 3
  • 3 false => true – Jaromanda X Commented Jan 15, 2018 at 4:50
  • developer.mozilla/en-US/docs/Web/API/XMLHttpRequest/… – Brian Commented Jan 15, 2018 at 5:03
  • Possible duplicate of JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..." – Andrew Marshall Commented Nov 4, 2018 at 18:26
Add a ment  | 

2 Answers 2

Reset to default 8

you just need to change

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

to

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

This will make the request operate asynchronously, meaning the code after it is sent will execute immediately instead of waiting for the request to plete. You can then use an event listener to tell the request what code to run when it's plete.

request.onreadystatechange = function () {
    if(request.readyState === XMLHttpRequest.DONE) {
        console.log("Request pleted!");
    }
}
request.open('GET', 'http://www.mywebsite.', 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.

发布评论

评论列表(0)

  1. 暂无评论