dtwbp26022 2011-11-01 21:18
浏览 48
已采纳

codeigniter - jquery登录函数不调用/返回

I have a login function in my controller that was getting called before, but i did some changes and it all just stopped.

I have a basic login with 2 inputs for a username/password and a submit

//Javascript

function initLogin(e) {
    e.preventDefault()
    var d = {
        user: $('#userName').val(),
        password: $('#password').val()
    }

    console.log('test')//gets returned

    $.post('/cms/login/',d,function(data) {
        console.log(data);//nothing returning
    },'json');
}

//Controller

class CMS extends CI_Controller {
    function __construct() {
        parent::__construct();

        $this->load->helper('url');
        $this->load->library('session');

        //$this->_check_if_logged_in();
    }

    private function _check_if_logged_in() {
        if (!$this->uri->segment(2) || $this->uri->segment(2) == 'login') return FALSE;
        if ($this->session->userdata('logged_in') != TRUE) redirect('/cms');
    }

    public function index() {
        if ($this->session->userdata('logged_in') == TRUE) redirect('/cms/home', 'refresh');

        $this->load->view('cms/log_in_view');
        $this->load->view('cms/parts/footer');
    }

    function login() {
        echo json_encode(array('test' => 'test'))//doesn't get echoed

        $data['user_name'] = $this->input->post('user');
        $data['pass_word'] = sha1($this->input->post('password'));

        $this->db->select('id');
        $res = $this->db->get_where('login',$data,1)->row_array();

        if (is_numeric($res['id'])) {
            $data['logged_in'] = TRUE;
            $this->session->set_userdata('logged_in',TRUE);
        }

        echo json_encode($res);
    }
}

Basically i commented out everything that I thought might get in the way of returning a response, but this is going against everything I've learned.

I'm convinced the login function in the controller doesn't get called, even though it did before when i didn't change anything in the js or login function itself

  • 写回答

2条回答 默认 最新

  • douqiao6015 2011-11-01 21:45
    关注

    One flaw I see is that if you find no row, $res['id'] will make codeigniter print out a HTML error, which makes your json echo not work properly.

    Try if (!empty($res)) instead.

    You can check with the Firebug Console (Addon in Firefox) what the ajax-request gave for an answer to see if more then the json was echo'd.

    Edit:

    Noticed that's not the real problem. Will read it again.

    Try changing class CMS extends CI_Controller to class Cms extends CI_Controller

    That is CMS -> Cms

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

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站