• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error when call JSON response

#1
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" ...
   
____
when code changed to

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

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

no error
Reply

#2
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.