views:

223

answers:

2

Hi, I am loading a image trough javascript:

myimage.src = "/mycontroller/mymethod";
myimage.alt = "image";
myimage.onload = function(){
    $('body').append(myimage);
};    

But, somethimes my controller method should return an error, with the http status 500. So, the content-type will not be "image/png" anymore. Will be text. There's a way to identify this error when loading the image, get the error message and display in alert?

Thanks.

+1  A: 

If the method is not guaranteed to succeed, a better solution would be for your method to return Javascript code which either loads an image, or displays an error. As far as I know, there is no way to tell if a URL to an image is actually an image, or if it's text data. I would love it if someone else provides an answer that proves me wrong!

Josh
I think this is the best way. There are `error` and `load` events for images but they are handled differently across browsers IIRC - some will file `load` only on a successful load but not on an error, but not trigger an `error`.
Pekka
A: 

Thanks guys, so, here is where we are right now: I have two choises: 1) My method always return javascript code, or to load an image in case of success (in that case, I must send the image within the javascript. with base64 or what else, because of my business rules). It's possible? 2) Use the error event (this one I didn't know about)