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

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan