簡體   English   中英

如何在網頁上顯示JSON中的數據?

[英]How can I display the data from the JSON on my webpage?

我正在編寫一個發送ajax請求的腳本。 雲似乎響應了JSON,但是如何在我的網頁上顯示JSON中的數據?

這里是漂亮打印的JSON的鏈接。

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <button onclick="myFunctionPost()">Start</button> <script> function myFunctionPost() { jQuery.ajax( { url: 'https://iotmmss0018275632trial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/app.svc/T_IOT_77877E443B666B7FED2F?$format=json', type: 'POST', crossDomain: true, dataType: 'jsonp', success: function( response ) { console.log(response); }, error : function(error) { console.log(error); } } ); } </script> </body> </html> 

通過var responseObject = json.parse(response)來制作一個javascript對象。

然后像使用JS對象一樣進行操作? 在不知道您要顯示什么HTML內容的情況下,很難說出確切的代碼。

為此,您可以使用JSON.stringify()空間參數。 您還需要使用<pre> </pre>包裝輸出,以保留行距。

 function myFunctionPost() { $.ajax( { url: 'https://jsonplaceholder.typicode.com/posts', type: 'GET', success: function(response) { $('#pp').html('<pre>' + JSON.stringify(response, undefined, 4) + '</pre>'); }, error: function(error) { console.log(error); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <button onclick="myFunctionPost()">Start</button> <p id="pp"></p> </body> </html> 

資料來源: jQuery是否具有類似於PHP的var_dump的JSON / javascript對象到HTML漂亮的打印功能?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM