Hi, guys. I'm new to Ajax, so I'm having a problem. When I press the supposed link to use Ajax, it shows an error message with the error parameter empty. What am I supposed to do?
I already tried to use base_url instead of site_url and it's no use. The structure of the click function is ok, I already tested it.
Thanks.
view.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js">
</script>
</head>
<body>
<div id="barra_horizontal">
</div>
<div id="barra_vertical_esquerda">
<ul id="nav">
<?php
echo "<li><a href='' id='ntop' name='ntop'>Ntop</a></li>";
foreach ($links as $l)
{
echo '<li>' . anchor($l['link'],$l['nome'], array('id'=>$l['id'])) . '</li>';
}
?>
</ul>
</div>
<div id="content">
<?php
if (isset($content))
{
include ($data[content]);
}
?>
</div>
<script>
$('#ntop').click(function(){
$.ajax({
url: '<?php echo site_url('home/get_all_table/');?>',
type:'POST',
dataType: 'json',
success: function(output_string){alert("Here!");},
error: function (x, status, error) {
alert("An error occurred: " + status + "
Error: " + error);
}
});
});
</script>
</body>
</html>
home.php
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
if ($this->session->userdata('logged_in'))
{
if (!$this->input->is_ajax_request())
{
$session_data = $this->session->userdata('logged_in');
$data['links'] = array(
array('link'=>'/home/logout/','nome'=>'Logout','id'=>'logout')
);
$this->load->view('main_view', $data);
}
else {
$data['links'] = array(
array('link'=>'/home/logout/','nome'=>'Logout')
);
$data['content'] = 'abcd.php';
$this->load->view('main_view', $data);
}
}
else
{
redirect('/','refresh');
}
}
function get_all_table()
{
$this->load->model('usuario_model');
$this->usuario_model->get_all_table();
}
}