0

I have a problem where I want to communicate on my own server between port 80 and port 8080 using an ajax request. I am aware of CORS and cross domain request origin policy and iframe solution to this problem.

Out of curiosity I want to know if and how this can be done. if the requested information is called as a <img> tag with an SRC of the URL that needs to be called and the returned content is somehow parsed from the data that is recieved --

is this possible? if not, why and if yes how?

Thanks!

Community
  • 1
  • 1
DMin
  • 10,049
  • 10
  • 45
  • 65

2 Answers2

1

On the same domain, this is possible with a canvas: you draw the image to a canvas with canvas.drawImage, and then use canvas.toDataURL or canvas.toBlob to get the data on the canvas.

In a cross-domain situation, toDataURL and toBlob will be blocked if the canvas has been "tainted" with a cross-domain image. To overcome this restriction, you must:

  1. Serve the image with CORS headers, and

  2. Set the crossorigin attribute of the <img> (to either "anonymous" or "use-credentials", depending on whether the fetch should be done with cookies or not).

apsillers
  • 112,806
  • 17
  • 235
  • 239
0

No. While there are APIs to access image data (although I think they are all related to bitmaps loaded onto <canvas> rather than <img>), you can't access the data from a different origin because the Same Origin Policy still applies.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335