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
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.
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
Synchronous XHR is now in deprecation state. The recommendation is that developers move away from the synchronous API and instead use asynchronous requests.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With