Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Imgur API错误,如何使用XHR上传_Javascript_Jquery_Xmlhttprequest_Imgur - Fatal编程技术网

Javascript Imgur API错误,如何使用XHR上传

Javascript Imgur API错误,如何使用XHR上传,javascript,jquery,xmlhttprequest,imgur,Javascript,Jquery,Xmlhttprequest,Imgur,上面的代码是我用来通过phonegap上传图像文件的代码。但是我猜代码已经过时了,可以;不能使用最新的imgur API。这是由OAuth支持的。我可以知道如何修复它以便上传图像吗 你说得对。。。代码已经过时,因此我修复的方法是按照以下说明匿名上传图像: 1.-在FormData中,您只需附加图像,因此不应附加键 2.-你必须发送一个带有你的客户id的标题,我想你已经。。。我使用以下代码xhr.setRequestHeader('Authorization','Client ID FOO') 根

上面的代码是我用来通过phonegap上传图像文件的代码。但是我猜代码已经过时了,可以;不能使用最新的imgur API。这是由OAuth支持的。我可以知道如何修复它以便上传图像吗

你说得对。。。代码已经过时,因此我修复的方法是按照以下说明匿名上传图像:

1.-在
FormData
中,您只需附加图像,因此不应附加键

2.-你必须发送一个带有你的客户id的标题,我想你已经。。。我使用以下代码
xhr.setRequestHeader('Authorization','Client ID FOO')
根据文档,它必须在打开
XMLHttpRequest
之后,但在发送请求之前

3.-当您尝试获取链接时,您必须解析JSON以读取信息,链接位于名为
link
data
节点中,因此解析将为:
var link=JSON.parse(xhr.responseText).data.link

4.-您必须使用OAuth 2.0的新稳定API,因此上载图像的行应该如下所示:
xhr.open(“POST”https://api.imgur.com/3/image.json");。。。如您所见,它只是更改了版本号,而不是
upload
,它使用
image
,并且必须是
https
。。。供您参考,这是第一种建议的方法,另一种建议的方法也很有效,如下所示:
xhr.open(“POST”https://api.imgur.com/3/upload.json");

对于您的代码,我还假设您使用了Drag'n Drop的示例,因此函数应该如下所示:

    var fd = new FormData();
    fd.append("image", file); // Append the file
    fd.append("key", API_KEY);
    // Create the XHR (Cross-Domain XHR FTW!!!)
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "http://api.imgur.com/2/upload.json"); // Boooom!
    xhr.onload = function() {
        console.log(JSON.stringify(xhr.responseText));
        alert("Anything?");
        var img_url = JSON.parse(xhr.responseText).upload.links.original;
        console.log("Image url of the uploaded image" + img_url);
我鼓励你去看看,它非常友好,帮助你制作功能和东西。。。正如我所说的,这是匿名上传,如果你想与用户上传图像,你必须首先用用户和密码进行身份验证,使用令牌,然后刷新它们,我没有这样做,但一旦你了解了它的工作原理,这应该不会太复杂

希望能有帮助

function upload(file) {

    /* Is the file an image? */
    if (!file || !file.type.match(/image.*/)) return;

    /* It is! */
    document.body.className = "uploading";

    /* Lets build a FormData object*/
    var fd = new FormData(); 
    fd.append("image", file); // Append the file
    var xhr = new XMLHttpRequest(); // Create the XHR (Cross-Domain XHR FTW!!!) Thank you sooooo much imgur.com
    xhr.open("POST", "https://api.imgur.com/3/image.json"); // Boooom!
    xhr.onload = function() {
    // Big win!    
        var link = JSON.parse(xhr.responseText).data.link;
        document.querySelector("#link").href = link;
        document.querySelector("#link").innerHTML = link;



        document.body.className = "uploaded";
    }
    // Ok, I don't handle the errors. An exercice for the reader.
    xhr.setRequestHeader('Authorization', 'Client-ID FOO');
    /* And now, we send the formdata */
    xhr.send(fd);
}