如何将JSON.stringify打印到div或表中

时间:2022-11-06 06:52:16

how can i print JSON.stringify into div or table ?? or in custom section by id ??

如何将JSON.stringify打印到div或表?或者在id的自定义部分?

this my cod :

我的鳕鱼:

js

JS

$(document).ready(function() {
    $.ajax({
        type : 'POST',
        url : 'server.php',
        dataType:"json",
        success : function (data) { 
            $("#orders").html(JSON.stringify(data));
        }
    }); 
});

php

PHP

<?php 
    $db = new PDO('mysql:host=localhost;dbname=Contact', 'root', '');
    $statement=$db->prepare("SELECT * FROM myfeilds");
    $statement->execute();
    $results=$statement->fetchAll(PDO::FETCH_ASSOC);
    $json=json_encode($results);
    echo $json;
?>

2 个解决方案

#1


0  

you can use Angular / Kendo / knockout or any of this framework for resolve this. following example explain how to resolve this using kendo ui http://docs.telerik.com/kendo-ui/framework/templates/overview

您可以使用Angular / Kendo / knockout或任何此框架来解决此问题。以下示例说明如何使用kendo ui http://docs.telerik.com/kendo-ui/framework/templates/overview解决此问题

function getAjax(){
   // in here put your ajax call. in this time i hardcode some values
   var val = [];
   val.push({id:'1',name:'google'});
   val.push({id:'2',name:'yahoo'});
   val.push({id:'3',name:'fb'});
  return val;
}

$(()=>{
     
      var val = getAjax();
        //Get the external template definition using a jQuery selector
        var template = kendo.template($("#javascriptTemplate").html());

        //Create some dummy data
       

        var data = template(val); //Execute the template
        $("#input").html(data); //Append the result
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>
<div id='input'>  </div> 

<script id="javascriptTemplate" type="text/x-kendo-template">
        <table>
        # for (var i = 0; i < data.length; i++) { #
            <tr> 
            <td> #=  data[i].id #</td> 
            <td>#=  data[i].name # </td>
            </tr>
 
        # } #
        </table>
    </script>

#2


0  

JSON is not HTML:

JSON不是HTML:

$("#orders").html(JSON.stringify(data));

I guess you want:

我想你想要:

$("#orders").text(JSON.stringify(data));

Of course, it's not going to look nice because JSON is designed for computers, not people.

当然,它不会看起来不错,因为JSON是专为计算机设计的,而不是人。

#1


0  

you can use Angular / Kendo / knockout or any of this framework for resolve this. following example explain how to resolve this using kendo ui http://docs.telerik.com/kendo-ui/framework/templates/overview

您可以使用Angular / Kendo / knockout或任何此框架来解决此问题。以下示例说明如何使用kendo ui http://docs.telerik.com/kendo-ui/framework/templates/overview解决此问题

function getAjax(){
   // in here put your ajax call. in this time i hardcode some values
   var val = [];
   val.push({id:'1',name:'google'});
   val.push({id:'2',name:'yahoo'});
   val.push({id:'3',name:'fb'});
  return val;
}

$(()=>{
     
      var val = getAjax();
        //Get the external template definition using a jQuery selector
        var template = kendo.template($("#javascriptTemplate").html());

        //Create some dummy data
       

        var data = template(val); //Execute the template
        $("#input").html(data); //Append the result
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>
<div id='input'>  </div> 

<script id="javascriptTemplate" type="text/x-kendo-template">
        <table>
        # for (var i = 0; i < data.length; i++) { #
            <tr> 
            <td> #=  data[i].id #</td> 
            <td>#=  data[i].name # </td>
            </tr>
 
        # } #
        </table>
    </script>

#2


0  

JSON is not HTML:

JSON不是HTML:

$("#orders").html(JSON.stringify(data));

I guess you want:

我想你想要:

$("#orders").text(JSON.stringify(data));

Of course, it's not going to look nice because JSON is designed for computers, not people.

当然,它不会看起来不错,因为JSON是专为计算机设计的,而不是人。