CodeIgniter Forums
json_encode & header() problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: json_encode & header() problem (/showthread.php?tid=51229)



json_encode & header() problem - El Forum - 04-25-2012

[eluser]Unknown[/eluser]
I have a function that handles an ajax/json request and responds with a string containing html. It contains the following code.

Code:
header('Content-Type: application/json');
echo json_encode($response); //send the response to the caller


When I the ajax call is made a "Opening validateAdd" dialog box opens up where validateAdd is the name of the ajax handler. I have worked with json before but have not seen this problem. Any ideas?

If I remove the header line the ajax json request completes but with an error as the header shows plain text and the ajax code expected json.




json_encode & header() problem - El Forum - 04-25-2012

[eluser]Unknown[/eluser]
I also tried the following with the same result ...

Code:
$this->output
->set_content_type('application/json')
->set_output(json_encode($response));

This dialog box that pops up happens with both windows and mac browsers.


json_encode & header() problem - El Forum - 04-25-2012

[eluser]CroNiX[/eluser]
Do you have code that executes below your echo json_encode() that is perhaps outputting something or setting another header? try putting "exit();" immediately after you echo the json out.