3 回答
TA贡献1877条经验 获得超1个赞
在您的控制器中创建一个方法来回显 json 输出
public $json = array();
public function render_json()
{
$this->output
->set_status_header(200)
->set_content_type('application/json')
->set_output(json_encode( $this->json ));
}
并且在您的控制器内的任何方法中只需附加到$this->json数组:
$this->json['key'] = 'value';
然后渲染:
$this->render_json();
顺便说一句,如果您想在全球范围内使用它,只需将其移动到基本控制器即可。
例子
public function fetch()
{
$this->json['key'] = 'value';
$this->render_json();
}
现在,当您通过 ajax 调用此方法时,它将为您提供一个 json 对象,其中包含$this->json数组中的任何内容,在 js 中:
.done(function( response ) {
console.log(response.key); // = value
});
TA贡献1790条经验 获得超9个赞
您是否尝试过使用本机 php 对其进行简单编码?或者这里有什么问题?
我会这样做:
$data['json']=json_encode($data);
所以你可以在视图中使用 $json 。
...然后最终在 JS 前端使用 JSON.parse。
TA贡献1862条经验 获得超6个赞
我在控制器的每个方法中都使用了这行代码,而不是渲染视图:
$this->output->set_content_type('application/json')->set_output(json_encode($data));
这是控制器现在的样子:
class Posts extends CI_Controller {
public function __construct()
{
parent::__construct();
}
private function _initPagination($path, $totalRows, $query_string_segment = 'page') {
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = base_url($path);
$config['query_string_segment'] = $query_string_segment;
$config['enable_query_strings'] =TRUE;
$config['reuse_query_string'] =TRUE;
$config['total_rows'] = $totalRows;
$config['per_page'] = 12;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$this->pagination->initialize($config);
$limit = $config['per_page'];
$offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
return ['limit' => $limit, 'offset' => $offset];
}
public function index() {
//call initialization method
$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
public function byauthor($authorid){
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['posts'] = $this->Posts_model->get_posts_by_author($authorid);
$data['posts_count'] = $this->Posts_model->posts_by_author_count($authorid);
$data['posts_author'] = $this->Posts_model->posts_author($authorid);
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
public function post($slug) {
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['posts'] = $this->Posts_model->sidebar_posts($limit=5, $offset=0);
$data['post'] = $this->Posts_model->get_post($slug);
if ($data['categories']) {
foreach ($data['categories'] as &$category) {
$category->posts_count = $this->Posts_model->count_posts_in_category($category->id);
}
}
if (!empty($data['post'])) {
// Overwrite the default tagline with the post title
$data['tagline'] = $data['post']->title;
// Get post comments
$post_id = $data['post']->id;
$data['comments'] = $this->Comments_model->get_comments($post_id);
$this->output->set_content_type('application/json')->set_output(json_encode($data));
} else {
$data['tagline'] = "Page not found";
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
}
- 3 回答
- 0 关注
- 172 浏览
添加回答
举报