首先,根据
Retrieving binary file content using Javascript, base64 encode it and reverse-decode it using Python添加正确的mimetype到Ajax调用:
$.ajax({
url: "someurltoajpeg",
type: "GET",
headers: {
"Authorization" : "Basic " + btoa("user:pw")
},
xhrFields: {
withCredentials: true
},
mimeType: "text/plain; charset=x-user-defined"
}).done(function( data, textStatus, jqXHR ) {
$("#image").attr('src', 'data:image/jpeg;base64,' + base64encode(data));
}).fail(function( jqXHR, textStatus, errorThrown ) {
alert("fail: " + errorThrown);
});
然后使用base64Encode函数,而不是btoa:
function base64Encode(str) {
var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh