栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用jQuery的ajax方法作为blob检索图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用jQuery的ajax方法作为blob检索图像

您不能使用jQuery ajax来执行此操作,而是使用本机XMLHttpRequest来执行此操作。

var xhr = new XMLHttpRequest();xhr.onreadystatechange = function(){    if (this.readyState == 4 && this.status == 200){        //this.response is what you're looking for        handler(this.response);        console.log(this.response, typeof this.response);        var img = document.getElementById('img');        var url = window.URL || window.webkitURL;        img.src = url.createObjectURL(this.response);    }}xhr.open('GET', 'http://jsfiddle.net/img/logo.png');xhr.responseType = 'blob';xhr.send();

编辑

因此,回顾这个​​主题,似乎确实有可能使用jQuery 3做到这一点。

jQuery.ajax({        url:'https://images.unsplash.com/photo-1465101108990-e5eac17cf76d?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ%3D%3D&s=471ae675a6140db97fea32b55781479e',        cache:false,        xhr:function(){// Seems like the only way to get access to the xhr object var xhr = new XMLHttpRequest(); xhr.responseType= 'blob' return xhr;        },        success: function(data){ var img = document.getElementById('img'); var url = window.URL || window.webkitURL; img.src = url.createObjectURL(data);        },        error:function(){        }    });<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script><img id="img" width=100%>

要么

使用xhrFields设置responseType

    jQuery.ajax({ url:'https://images.unsplash.com/photo-1465101108990-e5eac17cf76d?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ%3D%3D&s=471ae675a6140db97fea32b55781479e', cache:false, xhrFields:{     responseType: 'blob' }, success: function(data){     var img = document.getElementById('img');     var url = window.URL || window.webkitURL;     img.src = url.createObjectURL(data); }, error:function(){ }        });<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>    <img id="img" width=100%>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/447069.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号