2017-07-10 5 views
0

Ich habe diese Methode, ich muss mich in der "Storage App" anmelden und die img in einem HTML-IMG anzeigen, aber nicht wissen, wie die zweite Erfolgsfunktion zu analysieren.Zugriff auf authentifiziertes Bild durch Ajax Erfolg

PD: Ich habe überprüft, dass ich diese Erfolgsmethode mit Konsolenprotokollen erreiche.

setTimeout(function() { 
    $.ajax({ 
    contentType: 'application/json', 
    data: JSON.stringify({ 
     "username": "username", 
     "password": "password" 
    }), 
    dataType: "json", 
    processData: false, 
    type: 'POST', 
    url: pathStorage + "/api/login", 
    async: false, 

    complete: function(data) { 
     var response = $.parseJSON(data.responseText); 

     $.ajax({ 
     type: 'GET', 
     url: pathStorage + "/web-app/files/" + id + "_imagen.jpg", 
     headers: { 
      "Authorization": response.token_type + " " + response.access_token 
     }, 
     success: function(data) { 

      //this dont works 
      $('#idImg').prop('src', 'data:image/jpg;base64,' + data) 
     } 

     }) 
    } 

    }) 
}, 1000) 
+0

Warum Sie JavaScript Versprechen nicht verwenden? –

+0

Was haben Sie in Ihrer 'Daten' Antwort? weil Sie möglicherweise den entsprechenden Schlüssel aufrufen müssen, wenn es ein Objekt ist –

+0

@ N.Ivanov das Problem ist, ich bin nicht in der Lage, die Daten zu sehen, wenn ich Briefträger für diese Anfrage verwendet, wurde die Antwort der IMG angezeigt. –

Antwort

0

Dank @Musa für die Antwort, konnte ich das img gesetzt, hatte aber fileapi.js ich meine eigentliche Code verlassen hier importieren

setTimeout(function() { 
    $.ajax({ 
    contentType: 'application/json', 
    data: JSON.stringify({ 
     "username": "user", 
     "password": "password" 
     }), 
    dataType: "json", 
    processData: false, 
    type: 'POST', 
    url: sessionRutaStorage + "/api/login", 
    async: false, 

    complete: function (data) { 
     var response = $.parseJSON(data.responseText); 

     var xhr = new XMLHttpRequest() 
     xhr.open('GET', storagePath + "/web-app/files/" + id + "_imagen.jpg", true); 
     xhr.setRequestHeader("Authorization", response.token_type + " " + response.access_token) 
     xhr.onreadystatechange = function() { 
      if (this.readyState == 4 &&this.status == 200) { 

       var img=document.getElementById('imgInfo'); 
       var url = window.URL || window.webkitURL; 
       img.src = url.createObjectURL(this.response); 
       img.setAttribute("src",url.createObjectURL(this.response)) 
       } 
      }; 

     xhr.responseType = 'blob'; 
     xhr.send(); 

    } 

}) 
}, 1000)