1

Possible Duplicate:
Getting BLOB data from XHR request

I try to get an image from my server dynamically, but the XMLHTTPRequest returns null in the response. The Google Chrome network tool tells me that he loaded the day1.jpg, but the blob variable is null...

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();

The output is:

Image retrieved
blob: null
Community
  • 1
  • 1
Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54

4 Answers4

4

The reason is a bug on the Chrome side (also available on v18). Reported here

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Zuuum
  • 1,495
  • 11
  • 18
1

why use ajax to load the image (as far as I know you can't http://www.mail-archive.com/discuss@jquery.com/msg00377.html)? You can just dynamically generate an image element and set the src attribute to the image on your server.

var i = new Image();
i.onload = function()
{
   //do the stuff you need to do once it loads here
}
i.src = "day1.jpg"; 
scrappedcola
  • 10,423
  • 1
  • 32
  • 43
0

Use the console to inspect the xhr object and see if it's being populated at all

Andrew Hall
  • 3,058
  • 21
  • 30
-1

I doubt that you can load images from the server dynamically, instead what you can do is update the image source dynamically and tell from whci location shoul the image be fetched/loaded

Abhidev
  • 7,063
  • 6
  • 21
  • 26
  • 2
    Why wouldn't he be able to? All it does it call a simple GET request (exactly like a browser would do if you typed in the URL) and returns the response (the image). – Snuffleupagus Mar 05 '12 at 18:03
  • and how is he supposed to use the returned data??? – Abhidev Mar 06 '12 at 05:57