如何将JSON.stringify打印到div或table?或者在id ??
的自定义部分中 这是我的鳕鱼:js
$(document).ready(function() {
$.ajax({
type : 'POST',
url : 'server.php',
dataType:"json",
success : function (data) {
$("#orders").html(JSON.stringify(data));
}
});
});
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;
?>
答案 0 :(得分:0)
您可以使用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>
答案 1 :(得分:0)
JSON不是HTML:
$("#orders").html(JSON.stringify(data));
我想你想要:
$("#orders").text(JSON.stringify(data));
当然,它不会看起来不错,因为JSON是专为计算机而非人设计的。