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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度