When tried to convert to base64 from url - getting CORS Issue in JS

前端 未结 1 1062
[愿得一人]
[愿得一人] 2020-12-22 13:15

when i tried to convert image url to base64 am getting CORS issue. Not sure what exactly need to do to get rid of this CORS issue

my code look like this



        
相关标签:
1条回答
  • 2020-12-22 13:48

    The CORS issue you are facing is because you are trying to access the www.dropbox.com domain from a different domain. The short answer is that you can't fix this from your own domain--it requires cooperation from the www.dropbox.com domain to fix this. You are the client in this case, and dropbox is the server, so as a client you can't tell the server what security settings to use. The server can opt in and could configure their site to allow your origin, but that isn't something dropbox is likely to do. You'll need to re-architect your approach. (also see Ways to circumvent the same-origin policy if you don't believe me when I say you need to re-architect your approach.)

    Check out https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS for more background:

    For security reasons, browsers restrict cross-origin HTTP requests initiated from scripts. For example, XMLHttpRequest and the Fetch API follow the same-origin policy. This means that a web application using those APIs can only request resources from the same origin the application was loaded from unless the response from other origins includes the right CORS headers.

    You could try using JavaScript to inject a script tag into your DOM that sets the type of the script tag to something other than JavaScript. Then you could have your code read the contents of that and make use of it...but that is a completely re-architected approach. See How does HTML tags work inside script tag? and https://stackoverflow.com/a/8578840/230055 if you want to look into that.

    0 讨论(0)
提交回复
热议问题