How img tag gets content over cors headers

2019-08-20 16:57发布

问题:

Why, when I use fetch, to load the data from the server with disabled response CORS headers, I'v expectedly got an error:
Failed to load http://www.imgworlds.com/wp-content/uploads/2015/12/18-CONTACTUS-HEADER.jpg: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63343' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
But image from img tag is presented

<script>
    fetch('http://www.imgworlds.com/wp-content/uploads/2015/12/18-CONTACTUS-HEADER.jpg').then(console.log);
</script>
<img src="http://www.imgworlds.com/wp-content/uploads/2015/12/18-CONTACTUS-HEADER.jpg">

回答1:

The cross-origin sharing standard does not include img tags, but XHR/fetch requests and some cases including drawing images to a canvas does.

For more info: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#What_requests_use_CORS