2013-10-02 76 views
0

我有下面的js代碼上傳圖像imgur使用身份驗證。Imgur API返回「undefined」

<script type="text/javascript"> 
    window.ondragover = function(e) {e.preventDefault()} 
    window.ondrop = function(e) {e.preventDefault(); upload(e.dataTransfer.files[0]); } 
    function upload(file) { 


     if (!file || !file.type.match(/image.*/)) return; 


     document.body.className = "uploading"; 


     var fd = new FormData(); 
     fd.append("image", file); 
     var xhr = new XMLHttpRequest(); 
     xhr.open("POST", "https://api.imgur.com/3/image.json"); 
     xhr.onload = function() { 
      var code = '[img]' + JSON.parse(xhr.responseText).data.link + '[/img]'; 
      var editor = eval('opener.' + 'clickableEditor'); 
      editor.performInsert(code); 
      javascript:window.close() 
     } 

     xhr.setRequestHeader('Authorization', '39-letter-client-secret-code'); 

     xhr.send(fd); 
    } 
</script> 

它不會將圖像上傳到imgur,但會返回數據「undefined」。如果我在這裏錯過了某些東西,你能否提到我?

回答

0

我在這一行看到過一個問題;

xhr.setRequestHeader('Authorization', '39-letter-client-secret-code'); 

我改變了它;

xhr.setRequestHeader('Authorization', 'Client-ID 15-letter-Client-ID'); 

它的工作原理。抱歉打擾你。