dongzhizhai4070 2013-06-18 13:58 采纳率: 0%
浏览 38
已采纳

CodeIgniter Ajax空错误

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();
    }
}
  • 写回答

3条回答 默认 最新

  • dpglo66848 2013-06-18 14:04
    关注

    When you make an AJAX request, it's to a URL. Just like if you were to load that URL manually in your browser.

    The URL usuario_model/get_all_table is not a correct URL. In CodeIgniter, you cannot call the model directly. You need to call a controller, and have that controller call the model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析