That sounds reasonable, a common problem when ajaxing in stuff without checking the return data before presenting it to the browser. There are several java techniques that can avoid this issue, type cast the expected return value is one of them, the other most obvious method would be to inspect the data first (but that may slow it down too much, it would need to be tested).
Yeah, you can't do that with image elements though. When you set the src attribute you don't get to inspect the response before the browser tries to use it.