html Imgur:如何绕过本地主机上的跨源策略?

dgiusagp  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(134)

这是我们的代码(使用 AJAX 和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;

他抛出了以下错误:

[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."

我尝试了在localhost上可以使用的所有方法,但我们无法在服务器上运行它,因此必须使用localhost

xe55xuns

xe55xuns1#

您可以在禁用该部分安全性的情况下运行Chrome(或其他浏览器)。
所以

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

或类似产品:-)
请注意,这只是一个用于开发的解决方案。您仍然需要在生产中添加CORS标头。

相关问题