代码之家  ›  专栏  ›  技术社区  ›  Aleksandar K.

如何在网页上显示JSON中的数据?

  •  0
  • Aleksandar K.  · 技术社区  · 7 年前

    我正在编写一个发送ajax请求的脚本。云似乎用JSON响应,但我如何在我的网页上显示JSON中的数据?

    Here

    <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>
    2 回复  |  直到 7 年前
        1
  •  0
  •   Andrzej Smyk    7 年前

    为了实现这一点,您可以使用 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>

    Does jQuery have a JSON/javascript object to HTML pretty print function similar to PHP's var_dump?

        2
  •  0
  •   CodeSmith    7 年前

    通过 var responseObject = json.parse(response) 创建javascript对象。

    如果不知道要在什么HTML中显示什么,很难说出确切的代码。