0

This is our code for this (using ajax and jquery):

var image = new Image();
        image.onload = function () {
            screenResolution = JSON.parse(screenResolution);
            const width = (screenResolution.x);
            const height = (screenResolution.y);
            canvas.width = width;
            canvas.height = height;
            context.drawImage(image, 0, 0);
            const imageConvBase = canvas.toDataURL("image/jpeg", 1);
            
            $.ajax({
                    url: "https://api.imgur.com/3/upload",
                    method: "POST",
                    timeout: 0,
                    headers: {
                        Authorization: 'Client-ID {REMOVED FOR STACKOVERFLOW}'
                    },
                    data: {
                        image: imageConvBase,
                        type: "base64",
                    },
                    processData: false,
                    mimeType: "multipart/form-data",
                    contentType: false,
                    error: function(response) {

                        console.log("AJAX Error: " + JSON.stringify(response));

                    },
                    success: function(response) {
                        if (response.success) {
                            console.log(response.data.link);
                        }
                    }
                });
        }
        image.src = imgURL;

He throws me the following error:

[1124/143540.368:INFO:CONSOLE(0)] "Access to XMLHttpRequest at 'https://api.imgur.com/3/upload' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

I tried everything which is possible on localhost, but we cannot run it on a server so it has to be localhost

sckindvl
  • 1
  • 2
  • 1
    Does this answer your question? [Upload image to imgur failed because of cors](https://stackoverflow.com/questions/55733271/upload-image-to-imgur-failed-because-of-cors) – Yogi Nov 24 '22 at 13:52

1 Answers1

0

You can run Chrome (or other browsers) with that part of security disabled. See https://alfilatov.com/posts/run-chrome-without-cors/

So

[PATH_TO_CHROME]\chrome.exe" --disable-web-security

or similar :-)

Note this is only a solution for development. You'd still need to add CORS headers in production.

BaronVonKaneHoffen
  • 1,902
  • 1
  • 21
  • 29