CodeIgniter Forums

Full Version: error when call JSON response
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I don't know this is bug or not. But ...

I have Controller A with method like :

Code:
public function myjson() {
     ....
     ....

     return $this->respond($json); // output JSON    
}

when call : http://localhost:8080/a/myjson
showing the output, JSON

but the problem is, when call by other controller show Error like
"Call to a member function setContentType() on null" ...
[attachment=1861]
____
when code changed to

Code:
public function myjson() {
     ....
     ....

     return json_encode($json); // output JSON    
}

no error
If you want to return JSON from your controller method you should set JSON to response and return it - https://codeigniter.com/user_guide/outgo...the-output

PHP Code:
return $this->response->setJSON($json); 

Besides, variable $json may be an array (or maybe, it should be) - it is converted automatically to JSON.