deprecation Xmlhttprequest

Asked

Viewed 549 times

1

I would like to know how to fix my code XMLHttpRequest, he’s giving me this message [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. when I try to take the size of a img in bytes. NOTE: I tried to change this: request.open("HEAD", imgURL, false); for: request.open("HEAD", imgURL, true);, however the return of the size of the img is 0 when I do it. below follows the full javascript function

function getImageSizeInBytes(imgURL) {
      var request = new XMLHttpRequest();
      request.open("HEAD", imgURL, false);
      request.send(null);
      var headerText = request.getAllResponseHeaders();
      var re = /Content\-Length\s*:\s*(\d+)/i;
      re.exec(headerText);
      return parseInt(RegExp.$1);
  }

how do I resolve this message of deprecation returning?

  • Dear Leonardo, in Javascript using synchronous things is almost always unnecessary, understanding well what a callback is (I RECOMMEND reading: https://answall.com/a/45721/3635) you can easily adapt everything, an asynchronous example of how to catch the weight of the remote file: https://gist.github.com/brcontainer/b68ca1a9e850f9c1962defa424986759

  • I am trying to use as in the example that Voce posted but does not return me the error, but returns me 0bytes in the value of img

  • Trade the regex for /Content\-Length:(\s+)?(\d+)/i and in Javascript by done(parseInt(RegExp.$2));, see if it solves.

  • I discovered the problem but n I could solve yet, it is with the right size in the console.log() but I’m able to catch it to show along with the plugin Dropzone.js

  • @Guillhermenascimento because I can’t catch the size value? I can only show it with console.log() I am assigning in a variable and it is not returning me the value

  • Dear Leonardo, because you probably still don’t understand how callback works, see the link from my first comment. You have to move everything inside the function in the second parameter, which is explained in the answer: https://answall.com/a/45721/3635

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.