0

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;
 },
})
Rory McCrossan
338k41 gold badges321 silver badges353 bronze badges
asked Oct 8, 2021 at 15:10
10
  • 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. Commented Oct 8, 2021 at 15:11
  • What about missing headers Commented 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. Commented Oct 8, 2021 at 15:19
  • yes. how do i add missing headers and where ? Commented Oct 8, 2021 at 15:20
  • You cannot add them on the frontend Commented Oct 8, 2021 at 15:21

2 Answers 2

1

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.

answered Oct 8, 2021 at 15:16
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Oct 8, 2021 at 16:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.