这是我们的代码(使用 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
1条答案
按热度按时间xe55xuns1#
您可以在禁用该部分安全性的情况下运行Chrome(或其他浏览器)。
所以
或类似产品:-)
请注意,这只是一个用于开发的解决方案。您仍然需要在生产中添加CORS标头。