CodeIgniter Forums
Codeigniter, Jquery and Json communication proble, - 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: Codeigniter, Jquery and Json communication proble, (/showthread.php?tid=57808)



Codeigniter, Jquery and Json communication proble, - El Forum - 04-12-2013

[eluser]Unknown[/eluser]
I have the following problem, I have a Controller:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Json extends CI_Controller {

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

}
}

and a javascript file in which I call the controller,

Code:
$.get('http://localhost/tddd27/index.php/Json/index', function(data) {
                    console.dir(data);
                });

The problem is that the result is the following:
Code:
{"a":1,"b":2,"c":3,"d":4,"e":5}</html>

The result contains a </html> tag and I can not understand why. I am new to codeigniter, I just want to create a module that gets a product list from a database, a controller that generate a json representation of the products table, and a javascript file which call the controller and is called within a view


Codeigniter, Jquery and Json communication proble, - El Forum - 04-13-2013

[eluser]TheFuzzy0ne[/eluser]
You probably have some code somewhere that appends the trailing </html> to the end of the document when the buffer is sent to the browser. I have no idea where, however. Perhaps it's a post controller hook?

To get around that, you can probably just do:
Code:
echo json_encode($arr);
die();