Welcome Guest, Not a member yet? Register   Sign In
Codeigniter, Jquery and Json communication proble,
#1

[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
#2

[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();




Theme © iAndrew 2016 - Forum software by © MyBB