使用
fetch
方法添加 headers 然后把结果加载到
<img>
const src = 'https://api.mywebsite.com/profiles/123/avatar';
const options = {
headers: {
'Some-Header': '...'
fetch(src, options)
.then(res => res.blob())
.then(blob => {
imgElement.src = URL.createObjectURL(blob);
ajax + base64
需要通过异步请求设置请求头信息
使用 HTML5 APIs 把二进制数据转换为base64
把图片标签的src
设置为data:
协议和base64
数据
var oReq = new XMLHttpRequest();
oReq.open("GET", "yourpage.jsp", true);
oReq.setRequestHeader("Your-Header-Here", "Value");
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response;
if (arrayBuffer) {
var u8 = new Uint8Array(arrayBuffer);
var b64encoded = btoa(String.fromCharCode.apply(null, u8));
var mimetype="image/png";
document.getElementById("yourimageidhere").src="data:"+mimetype+";base64,"+b64encoded;
oReq.send(null);