views:

14

answers:

1

I'm trying to load an image with JQuery like so:

function loadImage( path ) {
 var img = new Image();
 $(img)
 .error(function () {
     alert('Error loading image');
 })
 .attr('src', path );
}

I'm wondering if I am able to change the Accept HTTP header before the image is loaded because the API for the images requires it.

+1  A: 

No, you don't get to set request headers on loading a plain Image. (After all, only the browser knows what image types it supports.)

You can set request headers on an XMLHttpRequest, but then even if you aren't affected by the cross-domain limitation, you face the difficulties of (a) getting binary response data out of it, and (b) loading that into an image. Both of these have various problems that make them impractical to do in a cross-browser way.

You will probably need to use your own server as a proxy to communicate with the remote service.

bobince