![]() |
JSON formatting in the browser (missing headers)? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: JSON formatting in the browser (missing headers)? (/showthread.php?tid=78510) |
JSON formatting in the browser (missing headers)? - blaasvaer - 02-01-2021 I'm responding from an API using this: Code: return $this->setResponseFormat('json')->respond(json_encode($response), 200); Now, I would 'expect' the proper headers would be set 'automagically' by doing this ... but NOTHING but the data is sent to the browser (no headers for some reason). What is the 'hack' I need to use to make CI4 send the proper headers without me having to muck around with it? This is extending the RerourceController ... if that matters. RE: JSON formatting in the browser (missing headers)? - iRedds - 02-01-2021 https://codeigniter.com/user_guide/outgoing/api_responses.html?highlight=respond#class-reference PHP Code: return $this->setResponseFormat('json')->respond(['error' => false]); Parameters: $data (mixed) – The data to return to the client. Either string or array. $statusCode (int) – The HTTP status code to return. Defaults to 200 $message (string) – A custom “reason” message to return. RE: JSON formatting in the browser (missing headers)? - blaasvaer - 02-01-2021 Thanks, but that's just what I already did. This does NOT send a header of type json to the client. I had to do this for that to work: return $this->response->setJSON($this->model->find($id)); Now, to me that is a completely different way to do things ... and again; the docs are confusing. RE: JSON formatting in the browser (missing headers)? - iRedds - 02-01-2021 PHP Code: return $this->setResponseFormat('json')->respond(['error' => false]); PHP Code: // setResponseFormat($format) example code with browser output (pic, CI4.1 php 7.3) https://yadi.sk/i/VBFIGIaF-TLF2g You can actually use setJSON() if you're comfortable. I'm just showing that the example from the documentation works. And by the way, when you passed the JSON string in the first example, the header was setted to text/html link to code |