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!
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.
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:
Serve the image with CORS headers, and
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).