How to convert image(static openstreetmap) into base64 string using javascript

I need to convert a static map image.I found the following code:

function toDataURL(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.onload = function() {
    var reader = new FileReader();
    reader.onloadend = function() {
      callback(reader.result);
    }
    reader.readAsDataURL(xhr.response);
  };
  xhr.open('GET', url);
  xhr.responseType = 'blob';
  xhr.send();
}

toDataURL('https://www.gravatar.com/avatar/d50c83cc0c6523b4d3f6085295c953e0', function(dataUrl) {
  console.log('RESULT:', dataUrl)
})

The problem is that any other image is generated very well, but from this link:

http://staticmap.openstreetmap.de/staticmap.php?center=52.6240,20,3744&zoom=10&size=640×640&maptype=mapnik&format=png

This does not work 🙁

Link to items such as DOM works, but this conversion is not working. Do you have any idea what’s wrong with my link?

Thanks for any replies 🙂


Source: stackoverflow-javascript