我有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");
});
在$。Get函数我得到了一个结果在我的div未定义,当使用"json",我什么也没得到。
在控制台上,我得到了一个警告:
主线程上的同步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>
让我换个说法:
$.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不是一个数组,例如,也许您需要一个不同的语法来处理它。
GrafiCode Studio是对的!当您返回JSON时,您应该将"print_r"更改为普通的"print"。没发现那个!