I want to get a base64 encoded image from another domain. I have enabled CORS on the backend but I am getting an error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://codedecoder.pythonanywhere.com/media/embed/2021/10/07/temp.jpg. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<img id="datadiv"></img>
var src = 'https://codedecoder.pythonanywhere.com/media/embed/2021/10/07/temp.jpg'
$.ajax({
 type: "GET",
 url: `https://codedecoder.pythonanywhere.com${src}`,
 crossDomain: true,
 success: function(dayta) {
 console.log(dayta);
 $('#datadiv')[0].src = dayta;
 },
})
 - 
 That error means that you have not configured CORS correctly on the server. This is 100% a server-side problem. If CORS is configured correctly, you do not need to make any JS changes.Rory McCrossan– Rory McCrossan2021年10月08日 15:11:39 +00:00Commented Oct 8, 2021 at 15:11
 - 
 What about missing headersRoyalBosS– RoyalBosS2021年10月08日 15:17:25 +00:00Commented Oct 8, 2021 at 15:17
 - 
 I'm not sure what you mean? If you're referring to the error, it means that the response coming back from the server is missing the necessary CORS headers.Rory McCrossan– Rory McCrossan2021年10月08日 15:19:17 +00:00Commented Oct 8, 2021 at 15:19
 - 
 yes. how do i add missing headers and where ?RoyalBosS– RoyalBosS2021年10月08日 15:20:37 +00:00Commented Oct 8, 2021 at 15:20
 - 
 You cannot add them on the frontendMike Shum– Mike Shum2021年10月08日 15:21:04 +00:00Commented Oct 8, 2021 at 15:21
 
2 Answers 2
You set CORS headers on the server side. It's not a client side problem. But, for development purposes, you can use Chrome's CORS extension. Look it up on the chrome extension store.
Comments
For Django, check out the django-cors-headers package:
https://github.com/adamchainz/django-cors-headers
It is maintained by a member of the Django Software Foundation technical team.