Nitin Agrawal
Nitin Agrawal

Reputation: 1361

image from cross domain showing fine in HTML but ajax calls for same image failing from browser console

Our static content server is serving images for various web portals (black box for me). On web portal all images are coming fine even though they are from different domain (assuming static server sets http headers accordingly). However if I try to access same image using browser console via ajax calls (using jquery or xmlhttp) it gives cross domain call failure error (i.e. request is successful but browser denied response). Below is a simple jsfiddle to show the problem

JSfiddle for image coming in dom but ajax call failing

/*Image tag works fine*/
<img src='https://casinogames.bwin.com/htmllobby/images/gameicon/melonmadness.jpg' /> 
/*ajax call fails*/

var a = $.ajax(' https://casinogames.bwin.com/htmllobby/images/gameicon/melonmadness.jpg');

I verified request/response headers and they are exactly same in both scenarios. I want to know if there is any specific difference between request from image tag and ajax? I tried both IE console and Chrome console and same results.

Upvotes: 0

Views: 2889

Answers (2)

Quentin
Quentin

Reputation: 943569

I want to know if there is any specific difference between request from image tag and ajax?

There won't be anything significantly different about the request, but either way you do it, the browser will not make the image data available to JavaScript when the request is a cross-origin one.

Displaying an image to the user from an img element is not a security risk.

Giving JavaScript written by a third party access to data from another server is a security risk.

Upvotes: 2

Azadrum
Azadrum

Reputation: 746

You cannot make ajax calls from different domain in normal ways.

here is a discussion about it.

You can look it up as "cross domain ajax calls"

Edit

Show remote img via jquery like...

var a = $('img').prop ('src', 'http://placehold.it/10x10');

Upvotes: 0

Related Questions