【发布时间】:2015-06-26 18:38:52
【问题描述】:
-----更新-----
我注释掉了window.URL.revokeObjectURL( imgSrc );,现在该调用适用于所有浏览器。 Chrome 中的 url 似乎被撤销得太早了。我仍然很想知道为什么会这样,并且想知道我现在处理撤销 URL 的方式是否存在任何问题。我现在撤销最后一个 URL,因为下一个 URL 加载了 if (imgSrc) {window.URL.revokeObjectURL( imgSrc );}(imgSrc 现在是一个全局变量)。
我有一个使用 AJAX 调用 CGI 脚本的网站,该脚本输出图像/jpeg 数据,然后使用 createObjectURL 函数将 blob 响应设置为页面上图像的 src。目前它适用于除 Chrome 之外的所有浏览器。
在所有其他浏览器中显示图像,但在 Chrome 中我收到消息:Failed to load resource: the server responded with a status of 404 (Not Found) 后跟以 blob: 为前缀的 url。
我尝试使用 webkitURL 代替,但这给了我同样的错误,然后是 webkitURL is deprecated。
这是我的 AJAX 调用的代码:
var filepath = "/babelia.cgi";
xmlhttp=new XMLHttpRequest();
xmlhttp.onload = function(oEvent) {
if (imgSrc) {window.URL.revokeObjectURL( imgSrc );}
var blob = xmlhttp.response;
if (endless) {
imgSrc = (window.URL ? URL : webkitURL).createObjectURL( blob );
document.getElementById("palette").src = imgSrc;
// window.URL.revokeObjectURL( imgSrc );
babel();
}
}
xmlhttp.open("POST",filepath,true);
xmlhttp.responseType = "blob";
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var info = "location=" + postdata;
if (!landscape) {info += "&flip=portrait";}
xmlhttp.send(info);
您可以在这里查看网站:https://babelia.libraryofbabel.info/slideshow.html
【问题讨论】:
-
我注意到在 Chrome 中创建的 URL 中的第一个冒号被
%3A替换,而在其他浏览器中不会发生这种情况。我尝试添加imgSrc = imgSrc.replace(/https%3A\/\//,"https://");,将冒号放回网址中,但图像仍然不会出现,并且我收到了相同的错误消息。
标签: javascript ajax image cgi blob