2

I am using something like this to get an image via jquery ajax.

$.ajax({
   url: imageurl,
   type:'GET',
   contentType: 'image/png',
   success: success,
   error: error,
   timeout: 5000
});

I can see that my request headers include

Access-Control-Request-Headers:origin, content-type, accept

On the other hand, If i set up a img element like

<img src="imageurl" ...

and observe the request headers I don't see any "Access Control Request Headers".

Just wondering what is the reason for $.ajax() adding this header. Why is it adding this for a images which should be a valid Cross-site HTTP request. Will it be a good practice or even possible to remove this header?

zero7
  • 1,298
  • 8
  • 14
  • 1
    Rather than going to the trouble of setting up an ajax request, why don't you just load an image using the src. You can attach an onload event to handle the success function you have. Something similar to this: http://stackoverflow.com/questions/4285042/can-jquery-ajax-load-image – scrappedcola Apr 25 '12 at 22:57
  • @scrappedcola thanks but i need the timeout so i am unable to use load() – zero7 Apr 26 '12 at 01:11

1 Answers1

1

Anything accessed using an XMLHttpRequest will have these headers, whether it's an image or not. The crucial part is the origin of the request (a script rather than an 'img' tag).

This header is actually created by the browser, so, no, it wouldn't be possible to remove it via jquery.

Historically scripts were not allowed to perform cross-site HTTP requests, and these headers are part of the new 'cross-origin sharing' feature. See: https://developer.mozilla.org/en/http_access_control

Note that it may be possible to circumvent by using jquery to generate an 'img' tag, which could potentially be manipulated in the way you want. I haven't tried but it's worth a try..

zero7
  • 1,298
  • 8
  • 14
laher
  • 8,860
  • 3
  • 29
  • 39
  • 1
    I just would like to second the fact that by creating an image tag I was able to get both Chrome and Firefox to properly send the Origin head. Excellent call on your idea to generate an image tag. – dougBTV Mar 27 '14 at 00:34