admin管理员组文章数量:1333638
Hello im using codeigniter and then i echo out my output from the database in my controller and then in my view file i do this:
<script type="text/javascript">
$.getJSON('ajax/forumThreads', function(data) {
alert(data.overskrift);
});
</script>
but it dont show anything :S
my model file
function forumList()
{
$this->db->select('overskrift', 'indhold', 'brugernavn', 'dato');
$this->db->order_by('id', 'desc');
$forum_list = $this->db->get('forum_traad');
if($forum_list->num_rows() > 0)
{
return $forum_list->result();
} else {
return false;
}
}
my controller
function forumThreads() {
$this->load->model('ajax_model');
$data['forum_list'] = $this->ajax_model->forumList();
if ($data['forum_list'] === true)
{
echo json_encode($data['forum_list']);
$this->load->view('includes/footer', $data);
} else {
return false;
}
}
Hello im using codeigniter and then i echo out my output from the database in my controller and then in my view file i do this:
<script type="text/javascript">
$.getJSON('ajax/forumThreads', function(data) {
alert(data.overskrift);
});
</script>
but it dont show anything :S
my model file
function forumList()
{
$this->db->select('overskrift', 'indhold', 'brugernavn', 'dato');
$this->db->order_by('id', 'desc');
$forum_list = $this->db->get('forum_traad');
if($forum_list->num_rows() > 0)
{
return $forum_list->result();
} else {
return false;
}
}
my controller
function forumThreads() {
$this->load->model('ajax_model');
$data['forum_list'] = $this->ajax_model->forumList();
if ($data['forum_list'] === true)
{
echo json_encode($data['forum_list']);
$this->load->view('includes/footer', $data);
} else {
return false;
}
}
Share
Improve this question
edited Jan 6, 2011 at 17:33
treeface
13.4k4 gold badges52 silver badges57 bronze badges
asked Jan 6, 2011 at 16:05
oleole
311 gold badge1 silver badge2 bronze badges
2
- a good way to test for me has been to just try and visit the controller function "in the wild" just typing it in the browser and see what the output is, just so you know you are getting the output you expect. – jondavidjohn Commented Jan 6, 2011 at 16:17
-
Remove
$this->load->view('includes/footer', $data);
after thejson_encode
. JSON data cannot have anything before or after it. – gen_Eric Commented Jan 6, 2011 at 16:19
3 Answers
Reset to default 3$forum_list->result()
returns an array of results.
If you only want 1 row, use $forum_list->row()
, otherwise in the javascript, you'll need to loop through all the rows.
$.each(data, function(i,v){
alert(v.overskrift);
});
EDIT: When outputting JSON, do not print anything before or after. You need to remove $this->load->view('includes/footer', $data);
after the json_encode
. Also, controllers don't return anything.
EDIT 2: Replace if ($data['forum_list'] === true)
with if ($data['forum_list'] !== false)
. The ===
pares type, and an array is not a boolean.
Model:
function forumList()
{
$this->db->select('overskrift', 'indhold', 'brugernavn', 'dato');
$this->db->order_by('id', 'desc');
$forum_list = $this->db->get('forum_traad');
if($forum_list->num_rows() > 0)
{
return $forum_list->result_array();
} else {
return false;
}
}
Controller:
function forumThreads() {
$this->load->model('ajax_model');
$data['forum_list'] = $this->ajax_model->forumList();
if ($data['forum_list'] !== false) {
echo json_encode($data['forum_list']);
}
}
Try this:
//works only with php 5.3
echo json_encode($data['forum_list'], JSON_FORCE_OBJECT);
本文标签: phpcodeigniter JSONStack Overflow
版权声明:本文标题:php - codeigniter JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742343073a2456989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论