2012-03-05 166 views
0

可能重複:
Getting BLOB data from XHR requestXMLHTTPRequest的響應返回null

我試圖從我的服務器獲取的圖像動態,但XMLHttpRequest對象在響應返回null。谷歌Chrome網絡工具告訴我,他裝載的day1.jpg,但BLOB變量爲空...

var xhr = new XMLHttpRequest(), blob; 

xhr.open("GET", "day1.jpg", true); 

// Set the responseType to blob 
xhr.responseType = "blob"; 

xhr.addEventListener("load", function() { 
    if (xhr.status === 200) { 
     console.log("Image retrieved"); 
     blob = xhr.response; 
     console.log("blob: " + blob); 
    } 
}, false); 

// Send XHR 
xhr.send(); 

輸出是:

Image retrieved 
blob: null 
+0

如果它的返回狀態代碼200和實際運行存在不應該是一個問題上的鍍鉻側的錯誤。嘗試將console.log(「blob:」+ xhr.response)設置爲grins。 – Snuffleupagus 2012-03-05 17:54:56

+0

blob = xhr.response; console.log(「blob:」+ blob);與console.log(「blob:」+ xhr.response) – 2012-03-05 19:19:53

+0

*應該*完全一樣,就像上面的代碼*應該*一樣工作。 ; p – Snuffleupagus 2012-03-05 19:21:11

回答

4

的原因是(它也可V18)Reported here

+0

非常感謝! – 2012-03-05 19:18:50

+0

http://www.html5rocks.com/en/tutorials/file/xhr2/ < - 在這裏您可以看到解決方法。 – 2012-03-05 20:00:10

0

使用檢查XHR對象控制檯看看它是否在所有

+0

如果處理程序被調用,谷歌瀏覽器告訴我它已加載,爲什麼不應該填充? – 2012-03-05 17:53:07

+0

嘗試一下,然後查看對象中填充的內容。 – 2012-03-05 17:56:33

+1

響應爲空,狀態爲200,readyState 4和全部爲空。但我已經告訴過,在我的問題... – 2012-03-05 19:24:38

-1

我懷疑,你可以從動態服務器加載圖像被填充,而不是你可以做的就是更新的圖像源動態和WHCI位置告訴建議立即進行刪除圖像中獲取/加載

+2

他爲什麼不能?它所做的只是調用一個簡單的GET請求(就像瀏覽器會在URL中輸入一樣)並返回響應(圖像)。 – Snuffleupagus 2012-03-05 18:03:23

+0

以及他應該如何使用返回的數據? – Abhidev 2012-03-06 05:57:11

1

爲什麼使用ajax來加載圖像(據我所知不能http://www.mail-archive.com/[email protected]/msg00377.html)?您可以動態生成圖像元素,並將src屬性設置爲服務器上的圖像。

var i = new Image(); 
i.onload = function() 
{ 
    //do the stuff you need to do once it loads here 
} 
i.src = "day1.jpg"; 
+0

這可能與HTML5。最後我想加載一個pdf文件。 – 2012-03-05 19:14:53