Javascript 如何将数据返回到格式化方法中的Jquery.Get请求?
刚刚从Asp.net迁移到php 我正在学习一些与php mvc相关的教程 我目前面临的问题是将数据返回到ajax调用。数据即将到来,但仪表板视图也已附加到它 我的路线是:Javascript 如何将数据返回到格式化方法中的Jquery.Get请求?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,刚刚从Asp.net迁移到php 我正在学习一些与php mvc相关的教程 我目前面临的问题是将数据返回到ajax调用。数据即将到来,但仪表板视图也已附加到它 我的路线是: Options -MultiViews RewriteEngine On Options -Indexes RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
我正在发出jquery请求
$.get('http://localhost:44500/phpmvc/dashboard/xhrGetListItem',function(o){
console.log(o);
});
答复如下:
[{"id":"1","text":"hassaan khan"},{"id":"2","text":"we are working fine by the way and all is good hope you all are good too"},{"id":"3","text":"\r\n how day how days are going hope you all working fine"},{"id":"4","text":"\r\n how day how days are going hope you all working fine"},{"id":"5","text":"i am doing some thing extremely good. know that\r\ni am working on php.\r\n "}]
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="http://localhost:44500/phpmvc/public/css/default.css" />
<script type="text/javascript" src="http://localhost:44500/phpmvc/public/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="http://localhost:44500/phpmvc/public/js/custom.js"></script>
<script type="text/javascript" src="http://localhost:44500/phpmvc/views/dashboard/js/default.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//Custom Functions here
})
</script>
</head>
<body>
<div id="header">
Header
<br />
<a href="http://localhost:44500/phpmvc/index">Index</a>
<a href="http://localhost:44500/phpmvc/help">Help</a>
<a href="http://localhost:44500/phpmvc/dashboard/logout">Logout</a>
</div>
<div class="content">
Content
dashboard ... logged in
<br/>
<form id="dashForm" action="http://localhost:44500/phpmvc/dashboard/xhrInsert" method="post">
<textarea rows="4" cols="50" name="text"></textarea>
<input type="submit"/>
</form>
<br/>
<hr/>
<div id="listInsert"></div>
</div>
<hr />
<div id="footer">
© Footer
</div>
</body>
</html>
在仪表板模型中,我有方法定义
function xhrGetListComment(){
//$this->db get the database object
$sth = $this->db->prepare('SELECT * FROM data');
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();
$data = $sth->fetchAll();
//print(json_encode($data));
echo json_encode($data);
}
我如何只返回json对象?如果以后需要进行一些处理,可能不是最干净的方法,但在没有处理的情况下可以工作:add
exit()代码>在回显JSON数据之后。否则,您可能会对其感兴趣或与之相近的内容,以“结束”输出。如果您使用它,请小心:“输出缓冲区必须由ob_start()启动”(在您开始回显任何内容之前)。@blex exit()有效。我需要关闭连接并完成响应。谢谢,伙计
function xhrGetListComment(){
//$this->db get the database object
$sth = $this->db->prepare('SELECT * FROM data');
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();
$data = $sth->fetchAll();
//print(json_encode($data));
echo json_encode($data);
}