我使用crossOrigin从url加载图像,然后将其转换为base64。但是控制台日志错误消息显示:
SEC7120:[CORS]找不到源“ http://localhost:26221” Access-Control-Allow-Origin响应中的“ http://localhost:26221” 跨源图像资源的标头位于 'http://test.com/directory/photo/550157.jpg'。
然后,我尝试将照片网址更改为“ https://pbs.twimg.com/profile_images/924682339258470401/HbmOMfzW_400x400.jpg”,但未发现错误。可用
下面是我写的源代码。
convertImgToBase64('http://test.com/directory/photo/' + obj.Table1[0][0] + '.jpg', function(base64Img){
//console.log('IMAGE:',base64Img);
$('#img-'+obj.Table1[0][0]).attr('src', base64Img);
})
function convertImgToBase64(url, callback, outputFormat){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this,0,0);
var dataURL = canvas.toDataURL(outputFormat || 'image/png');
callback(dataURL);
canvas = null;
};
img.src = url;
}
请帮助我,谢谢。