Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How define json header in codeigniter controller?

I want to show response in json format in controller and don't want to move in view. So, please let me any way so that I can show json response.

Here is the code where i want to add json headers.

$result = $this->mod_doc->get_list();   
echo json_encode($result);
like image 337
Shalu Avatar asked Jun 10 '15 04:06

Shalu


2 Answers

Just give these a try.

public function signin() {
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);    

//add the header here
header('Content-Type: application/json');
echo json_encode( $arr );
}

or Try this way

return $this->output
        ->set_content_type('application/json')
        ->set_status_header(200) // Return status
        ->set_output(json_encode(array(your array)));
like image 54
Harigovind R Avatar answered Sep 23 '22 23:09

Harigovind R


$result = $this->mod_doc->get_list();
$this->output
    ->set_content_type('application/json') //set Json header
    ->set_output(json_encode($result));// make output json encoded

for more reference https://codeigniter.com/userguide3/libraries/output.html

like image 20
Mayank Tailor Avatar answered Sep 24 '22 23:09

Mayank Tailor