Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  66] [ 5]  / answers: 1 / hits: 25936  / 6 Years ago, mon, january 15, 2018, 12:00:00

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?


More From » html

 Answers
36

you just need to change


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

to


request.open('GET', 'http://www.mywebsite.com', 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 complete. You can then use an event listener to tell the request what code to run when it's complete.


request.onreadystatechange = function () {
if(request.readyState === XMLHttpRequest.DONE) {
console.log("Request completed!");
}
}

[#55459] Wednesday, January 10, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
debra

Total Points: 583
Total Questions: 111
Total Answers: 111

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
;