Easiest way to read an img with Jquery and return base64 encode

What is the easiest way to convert an image read via ajax-jquery to base64? I found this solution on stackoverflow but it doesn’t work, so I guess the solution is wrong.

$.ajax({
    url: "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", 
    type: "GET",
    crossDomain: true,
    success: async function(result, textStatus, jqXHR){
            // I want the base64 encoded version of my img
            console.log(btoa(unescape(encodeURIComponent(result))))       
    },  // success
    error: function(xhr, status){
        reject(img_url)
    } // error
})

Answer

Try this, it works for me:

return new Promise((resolve, reject) => { 
    $.ajax({
        url:'YOUR_URL',
        cache:false,
        xhr:function(){
            var xhr = new XMLHttpRequest();
            xhr.responseType= 'blob'
            return xhr;
        },
        success: function(data){
            var reader = new FileReader()
            reader.onload = () => resolve(reader.result)
            reader.onerror = reject
            reader.readAsDataURL(data);      
        },
        error:function(){
            
        }
    })
}