0

我正在尝试使用 jQuery 和 CodeIgniter 验证用户登录。如果找到该用户,他将被重定向到另一个页面,所以我在这里尝试的是在用户输入错误的用户名或密码时获取错误消息。这是我的主要目标。

在视图中,我有以下代码:

<?php
    $param = 'id="formLogin"';          
    echo form_open('backOffice/loginCheck', $param); 
?>
 <label for="username">* user name</label>
 <input type="text" name="username" id="username" />

 <label for="password">* password</label>
 <input type="password" name="password" id="password" />

  <input type ="submit" name="login" id="login" value ="LOGIN" />
  <?php echo form_close(); ?>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script>
  <script src="<?php echo base_url(); ?>backOffice/js/loginscript.js"></script> 

现在,这里是来自 loginscript.js 的代码

 btnLogin.on('click', function(errorMessage){
       if(userName.val()!='' &&  password.val() !=''){
          $.ajax({
            type: 'POST',
            url: 'backOffice/loginCheck',
            data: 'formLogin.serialize()',
            success: function(errorMessage){
                console.log('i never get here');
            },
            dataType: 'jsonp'
        });

        e.preventDefault();
        console.log(errorMessage);

       } 
   })

最后,这是来自控制器的代码

 public function loginCheck()
    { 

       // set the validation rules
        $this->form_validation->set_rules('username', 'Username', 'required|trim|encode_php_tags');
        $this->form_validation->set_rules('password', 'Password', 'required|trim|encode_php_tags');
        $this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
        if ($this->form_validation->run() != FALSE) 
        {

            $ids=array();
            $ids[0]=$this->db->where('username', $this->input->post('username'));
            $ids[1] = $this->db->where('password', md5($this->input->post('password')));
            $query = $this->backOfficeUsersModel->get();
            if($query)
                {
                    $data = array(
                    'username'       => $this->input->post('username'),
                    'isUserLoggedIn' => true
                    );    
                $this->session->set_userdata($data);
                $data['title'] = "Welcome to dashboard!";
                $data['main_content'] = 'dashboard';
                $this->load->vars($data);
                $this->load->view('backOffice/template');
        } else {   
            $errorMessage = "Wrong Username or Password";
            $this->cms(json_encode($errorMessage));
        }
        } else {

            // form validation fail, send the message
            // form validation to fail, mean that user has javascript disabled
            $errorMessage = "Wrong User Name OR Password.<br /> Please try again!";
           $this->cms(json_encode($errorMessage));
        }



    } // end of function loginCheck 
4

2 回答 2

0

我猜data: 'formLogin.serialize()',应该data: formLogin.serialize(),formLoginjQuery 对象将表单与您要发送的数据结合起来。看起来您的数据类型不应该jsonjsonp

你也有errorMessage作为你的事件对象,但尝试调用e.preventDefault()

于 2012-08-18T07:02:53.537 回答
0

您需要检查的第一件事是显示任何error code200响应的 ajax 请求。

第二件事btnLogin.on('click', function(errorMessage){看起来不对,因为这里errorMessage可能充当event.

第三件事在下面的代码中,errorMessage也用于成功响应。建议您更改变量名称。

btnLogin.on('click', function(errorMessage){
       if(userName.val()!='' &&  password.val() !=''){
          $.ajax({
            type: 'POST',
            url: 'backOffice/loginCheck',
            data: 'formLogin.serialize()',
            success: function(errorMessage){//here
                console.log('i never get here');
            },
            dataType: 'jsonp'
        });

        e.preventDefault();
        console.log(errorMessage);

       } 
   })

第四件事是loginCheck(),你正在做以下事情

$this->load->vars($data);
$this->load->view('backOffice/template'); // both link will echo the html part

$errorMessage = "Wrong Username or Password";
$this->cms(json_encode($errorMessage));// json encode works for php array, not for string

所以根据我loginCheck()的响应字符串而不是json。

最后添加error功能,ajax()以便您也可以看到错误。有关更多信息,请查看此

于 2012-08-20T05:40:42.393 回答