【发布时间】:2015-02-08 17:44:04
【问题描述】:
我在尝试远程上传到 imgur 时偶尔会收到此错误:加载资源失败:服务器响应状态为 403(禁止访问)。 但并非总是如此,这很可疑。
这是我的代码:
/* Drag and drop */
window.ondragover = function(e) {
e.preventDefault();
};
window.ondrop = function(e) {
e.preventDefault();
upload(e.dataTransfer.files[0]);
};
function upload(file) {
document.getElementById("view1").style.display = "none";
document.getElementById("loading").style.display = "inline";
var imageLink = "";
/* Is the file an image? */
if (!file || !file.type.match(/image.*/)) return;
document.body.className = "uploading";
var fd = new FormData();
fd.append("image", file); // Append the file
fd.append("key", "6528448c258cff474ca9701c5bab6927"); // The key
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://api.imgur.com/2/upload.json");
xhr.onload = function() {
var link = JSON.parse(xhr.responseText).upload.links.imgur_page;
document.getElementById("loading").style.display = "none";
document.querySelector("#link").href = link;
document.querySelector("#link").innerHTML = link;
var imageLink = ""+document.querySelector("#link").innerHTML.replace("http://imgur.com/", "http://i.imgur.com/")+".jpg";
/* Image Preview */
document.getElementById("result").style.display = "inline";
document.getElementById("link-to-image").style.background = "url(" + imageLink + ") center center no-repeat";
document.body.className = "uploaded";
};
/* Send the formdata */
xhr.send(fd);
/* Remove the button */
document.getElementById("button").remove();
};
【问题讨论】:
-
你确定你没有超过他们的速率限制吗?或者,这可能只是他们的临时问题(高服务器负载)。
-
@Nit 我确定我不是朋友,我只是在测试应用程序,所以我们说一小时上传 5-10 次。
标签: javascript jquery imgur