Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS: [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience [duplicate]

I'm receiving the error in my project trying an Ajax request

[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

function getReviews() {
var toReturn = $.ajax({
    url: 'API/reviews.json',
    async: false
}).responseJSON;
return toReturn;
}

I would like to know if there is a different way to write this to not have that error

like image 970
Jakub Avatar asked Jun 11 '17 19:06

Jakub


People also ask

How do I fix synchronous XMLHttpRequest on the main thread is deprecated?

The solution to the above problem is to set async setting of the jQuery AJAX function to true as AJAX itself means Asynchronous JavaScript and XML and hence if you make it Synchronous by setting async setting to false, it will no longer be an AJAX call.

Why is synchronous XMLHttpRequest deprecated?

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

Is XHR deprecated?

Synchronous XHR is now in deprecation state. The recommendation is that developers move away from the synchronous API and instead use asynchronous requests.


1 Answers

Synchronous XMLHttpRequest is very bad because they are blocking entire app while it's waiting for a response from the server so you never should be using them.

To make your request asynchronous remove async option and specify callback instead:

function getReviews(cb) {
  $.ajax({
      url: 'API/reviews.json'
  }).done(cb);
}

getReviews(function(data) {
  // Access your data here
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
like image 165
G07cha Avatar answered Sep 30 '22 06:09

G07cha