原生js的http请求调用写法(获取图片流并生成图片元素到页面)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var xhr = new XMLHttpRequest();    
xhr.open("get", 'http://api.btstu.cn/sjbz/zsy.php', true);
xhr.responseType = "blob";
xhr.onload = function() {
if (this.status == 200) {
debugger
var blob = this.response;
var img = document.createElement("img");
img.onload = function(e) {
window.URL.revokeObjectURL(img.src);
};
img.src = window.URL.createObjectURL(blob);
document.getElementsByTagName("body")[0].style.backgroundImage = img;
}
}
xhr.send();