2016-10-16 2 views
2
$.ajax({ 
     url: 'https://offcloud.com/api/cloud/status', 
     data: {'requestId' : 'theid'}, 
     type: 'POST', 
     crossDomain: true, // enable this 
    xhrFields: { 
     withCredentials: true 
    }, 
     success: function(data) { 
      var stringData =JSON.stringify(data); 
      el.innerHTML = "<pre lang='xml'>" + stringData + "</pre>"; 
      console.log(data); 
     }, 
     error: function() { console.log('Failed!'); } 
    }); 

Dies ist, was angezeigt wird:Wie man richtig AJAX angezeigten Post-Daten zurückgeben

{"data":[{"accountId":"5xxx","remoteOptionId":"yyyyyy","type":"gdrive","username":"[email protected]"}]} 

Allerdings mag ich es richtig im XML-Format angezeigt. Oder irgendein tatsächlich lesbares Format, eine Tabelle (vorzugsweise eine Tabelle). Gibt es einen einfachen Weg, dies zu tun?

+0

Verwenden Sie jQuery-Databases. – saurav

Antwort

0

Set Content-Type: application/xml in Anfrage

0

Wenn Sie das JSON-Format nicht mögen, Sie nicht JSON.stringify(data) brauchen, alles was sie tut, ist das zurückgegebene Objekt in die Zeichenfolge, die Sie sehen, um konvertieren schnell angezeigt werden kann.

In Ihrer success Funktion können Sie die einzelnen Werte wie folgt zugreifen:

var accountId = data[0].accountId; 

Oder, wenn das nicht funktioniert (weil ich bin nicht ganz sicher, warum Sie data in Ihrem Rückgabewert haben) , versuchen Sie folgendes:

var accountId = data.data[0].accountId; 

Dann könnten Sie manuell diese Werte in Ihrer Ausgabe einfügen wie folgt

"<accountId>" + accountId + "</accountId>"; // XML format 
"<td>" + accountId + "</td>"; // HTML table cell 

... oder finden Sie ein Framework oder eine Bibliothek, die es für Sie tun.