我的问题被标记为与this无关的重复。我正在尝试使用Blob响应类型并为其创建URL,而不是使用base64转换图像。
我想复制mithril.js中this帖子中提出的代码,这是我的版本:
var getimage = function() {
m.request({
method: "POST",
url: "http://localhost:8000/",
data: gui,
responseType: "blob",
extract: function(xhr) {return xhr},
})
.then( function(result) {
console.log(result) // displays request
imgSrc = URL.createObjectURL( result.response ); // after this...
// imgSrc is still set to undefined
})
}
我的请求实际上返回了一个jpeg图像,可以在控制台调试器中看到它(请求>响应),但是我的变量imgSrc
保留了undefined
的值。
这是我的调试控制台中的响应:
因此result.response实际上是一个二进制文件(jpeg图像),但是URL.createObjectURL
函数未创建任何blob。我是javascript新手(昨天开始),无法调试。
答案 0 :(得分:0)
由于错误,mithril.js尚无法实现。一个简单的解决方法是手动执行请求:
var getimage = function() {
var oReq = new XMLHttpRequest();
oReq.open("POST", 'http://localhost:8000/', true );
oReq.responseType = "blob";
oReq.onload = function ( oEvent )
{
URL.revokeObjectURL( imgSrc ); // revoke previous image source
var blob = oReq.response;
imgSrc = URL.createObjectURL( blob );
};
oReq.send( JSON.stringify(gui) );
}