关键
responseType:"blob",
原生js
var xhr = new XMLHttpRequest();
xhr.onload = function () {
if (xhr.status === 200) {
var img = document.getElementById('your_img_id');
const reader = new FileReader();
reader.onloadend = function() {
img.src = reader.result
}
reader.onerror = console.log;
reader.readAsDataURL(xhr.response);
} else {
console.error('请求失败');
}
};
xhr.open('GET', '/1.png', true);
xhr.setRequestHeader( Authorization:'',);
xhr.responseType = 'blob';
xhr.send();
axios
axios({
url:"/1.png",
method:"get",
responseType:"blob",
headers:{
Authorization:'',
}
}).then(res=>{
var img = document.getElementById('your_img_id');
const reader = new FileReader();
reader.onloadend = function () {
img.src = reader.result
}
reader.onerror = console.log;
reader.readAsDataURL(res.data);
})