假如后台的接口,我們本來是返回一個圖片,但是有些情況下,接口直接返回一個stream,那么如何通過AJAX顯示在頁面?
var xhr = new XMLHttpRequest(); xhr.open("get", "http://localhost:8080/getImage", true); xhr.responseType = "blob"; xhr.onload = function() { if (this.status == 200) { var blob = this.response; var img = document.createElement("img"); img.src = window.URL.createObjectURL(blob); $("#img").html(img); } }; xhr.send();
細節中特別的地方就是blob,通過這個類型來處理stream
