新编程.i现在我的json数据正确我想要显示它在html中。 这里是我的PHP代码显示收到json数据到html
<?php
header('Access-Control-Allow-Origin: *');
$con=mysqli_connect("host","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$callback=$_GET['callback'];
$result = mysqli_query($con,"SELECT * FROM demo");
$var= array();
while($row = mysqli_fetch_assoc($result))
{
$var[]=$row;
}
echo $callback."(".json_encode($var).")";
mysqli_close($con);
?>
ND这里是Java脚本& HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
fname = screen.width;
$.ajax({
type: 'GET',
url: "devangpatel.host56.com/sample.php?jsonp=processResults"+"fname="+fname,
dataType: 'jsonp',
jsonp: 'callback' ,//jquery will add callback
jsonpCallback:'processResults'//name of the callback function which server must return
});
window.processResults = function (response){
//it must be called with response data inside server answer
console.log(response);
}
</script>
</head>
<body>
<h2>AJAX</h2>
<div id="myDiv"></div>
</body>
</html>
也提出了一些网站,了解有关从服务器获取数据(AJEX。)
这又回到一个字符串,所以你只是试图让它无论如何显示在页面上?下面是你的页面上的样子,从服务器'{name:'first',age:10,lived:['Dallas','Boston']}' - 显然有不同的数据 – Deryck