我有3个文件。
1。)显示它的文件
<div id="tweet">
</div>
这是应该显示的地方。
2.)从数据库中获取数据的php文件
function ajaxGetTweets()
{
$stmtSelectTweets = $this->db->prepare('SELECT * FROM tweets');
$stmtSelectTweets->setFetchMode(PDO::FETCH_ASSOC);
$stmtSelectTweets->execute();
$data = $stmtSelectTweets->fetchAll();
print_r(json_encode($data));
}
这个函数在json中返回结果,当我使用print_r
时,我得到了正确的格式3。)ajax的js文件
$( document ).ready(function() {
$.get("homescreen/ajaxGetTweets", function( o )
{
for(var i = 0; i < o.length; i++)
{
$("#tweet").append("<div>"+o[i].tweet+"</div>");
}
}, "json");
});
离开&#34; json&#34;在$ .get函数结束时,我在div中得到了一个未定义的结果,当使用&#34; json&#34;时,我什么都没有。
在控制台中我得到了一个警告:主线程上的同步XMLHttpRequest因不推荐使用 它对最终用户的体验产生不利影响。如需更多帮助, 检查http://xhr.spec.whatwg.org/。
Output: <!DOCTYPE HTML>
<html>
<head>
<title>Twitter</title>
<link rel="stylesheet" href="http://localhost/mvc_tut/public/css/bootstrap.min.css">
<script type="text/javascript" src="http://localhost/mvc_tut/public/js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="http://localhost/mvc_tut/public/js/bootstrap.min.js"></script>
</script>
</head>
<body>
<div id="header">
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="http://localhost/mvc_tut/index">Startseite<span class="sr-only">(current)</span></a></li>
<li><a href="http://localhost/mvc_tut/ueberuns">Über uns</a></li>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
<div id="content">
ERROR
</div>
<div id="footer">
Footer
</div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<title>Twitter</title>
<link rel="stylesheet" href="http://localhost/mvc_tut/public/css/bootstrap.min.css">
<script type="text/javascript" src="http://localhost/mvc_tut/public/js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="http://localhost/mvc_tut/public/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://localhost/mvc_tut/views/homescreen/js/homescreen.js"></script>
</script>
</head>
<body>
<div id="header">
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="http://localhost/mvc_tut/homescreen">Home<span class="sr-only">(current)</span></a></li>
<li><a href="http://localhost/mvc_tut/notifications">Notifications</a></li>
<li><a href="http://localhost/mvc_tut/messages">Messages</a></li>
</ul>
<a class="navbar-brand" href="#">Brand</a>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Search</button>
<button type="submit" class="btn btn-default">Tweet</button>
</form>
<form class="navbar-form" action="homescreen/logout" method="post">
<button type="submit" class="btn btn-default">Logout</button>
</form>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
<div id="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div class="row">
</div>
<div class="row">
</div>
</div>
<div class="col-md-4">
<form class="form-control" id="tweetInsert" action="http://localhost/mvc_tut/homescreen/ajaxInsertTweet" method="post">
<textarea name="tweetText" rows="3" cols="40"></textarea>
<button type="submit" name="submitTweet">Tweet</button>
</form>
<div class="row">
<div id="tweet">
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
</div>
<div class="row">
</div>
</div>
</div>
</div>
</div>
<div id="footer">
Footer
</div>
</body>
</html>
答案 0 :(得分:0)
让我稍微改写一下:
$.getJSON("homescreen/ajaxGetTweets", function(o) {
var h="";
for(var i=0; i<o.length; i++) h+="<div>"+o[i].tweet+"</div>";
$("#tweet").html(h);
});
这种方式更好理解并且速度更快(因为您只编写一次内容,允许浏览器只渲染一次)。但你的版本也应该有效;如果没有,也许我们需要检查你的JSON的结构,看看那个领域是否真的存在,等等。
例如,如果返回的JSON不是数组,那么您可能需要使用不同的 for 语法来遍历它。
<强>更新强>
GrafiCode Studio 是对的!返回JSON时,应将“print_r”更改为普通的“print”。没发现那个!他应该得到分数......