用户名和密码通过ajax登录..无法登录

时间:2017-02-13 07:09:15

标签: php ajax

以下控制器代码显示无法登录。  没有错误得到什么问题我无法理解如何通过ajax调用

        public function loginn()
            {
                $email=$this->input->post('email');
                $password=$this->input->post('password');

                //$this->load->library('form_validation');

                $this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]');
                $this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[40]');

                if ($this->form_validation->run() && $this->Login_model->login($email, $password)) { 
                    $this->welcome();
                } 
                else {
                    $this->form_validation->set_message('check_database', 'Invalid username or password');

                    $this->index(); 

                       }
            }

下面显示的代码视图页面通过ajax.script发送post值,如下所示。使用ajax将post值传递给controller。

  <script type="text/javascript">
$(document).ready(function(){
    $("#fpassf").click(function(){
        //e.preventDefault();
        var email = $("#email").val();
        var password= $("#password").val();


        $.ajax({
            type: "POST",
            url: "<?php echo base_url() ?>"+"Index.php/Login_cntrl/loginn",
            data: {email:email,password:password},
            success:function(data)
            {
                alert('Successfully login');
            },
            error:function()
            {
                alert('fail');
            }
        });
    });
});


图像如下所示 enter image description here

点击下面显示的登录图片 enter image description here

2 个答案:

答案 0 :(得分:0)

您似乎正在使用Codeigniter Framework。因此,我对您的Ajax URL路径存在疑问。

你已经给出了

<script type="text/javascript">
    $.get('@Url.Action("GetData")', function (result) {
        Morris.Line({
            element: 'samplechart',
            data: result,
            xkey: 'period',
            ykeys: ['a', 'b'],
            labels: ['YES', 'NO'],
            xLabelAngle: 60,
            parseTime: false,
            resize: true,
            lineColors: ['#32c5d2', '#c03e26']
        });
    });
</script>

但在codeigniter中它应该像

 url: "<?php echo base_url() ?>"+"Index.php/Login_cntrl/loginn",

OR

 url: "<?php echo base_url() ?>"+"/Login_cntrl/loginn",

您也可以从脚本文件中调用此ajax。这不是一个php文件。所以base_url()函数不会在那里工作。在这种情况下,您必须以隐藏格式将base_url保存到输入变量中。然后应该获取你的ajax代码。

 url: "<?php echo base_url('Login_cntrl/loginn') ?>",

然后ajax一个

<input type="hidden" name="myurl" value="<?php echo base_url();">

答案 1 :(得分:0)

您必须将基本登录操作和ajax登录操作分开,因为它们都会发送不同的响应。

PHP控制器:

public function ajax_login()
{
    $email = $this->input->post('email');
    $password = $this->input->post('password');

    $this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]');
    $this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[40]');

    if ($this->form_validation->run() && $this->Login_model->login($email, $password)) { 
        return $this
            ->output
            ->set_status_header(200)
            // here you tell to the ajax that the login is successful
            ->set_output(json_decode(array('status' => 'success', 'message' => 'PUT_YOUR_SUCCESS_MESSAGE_HERE')))
        ;
    } else {
        return $this
            ->output
            ->set_status_header(200)
            // here you tell to the ajax that the login is failed
            ->set_output(json_decode(array('status' => 'error', 'message' => 'PUT_YOUR_ERROR_MESSAGE_HERE')))
        ;
    }
}

使用Javascript:

<script type="text/javascript">
    $(document).ready(function(){
        $("#fpassf").click(function(){
            var email = $("#email").val();
            var password = $("#password").val();

        $.ajax({
            type: "POST",
            url: "<?php echo base_url('Login_cntrl/ajax_login') ?>",
            data: {email:email, password:password},
            success: function(data) {
                data = JSON.parse(data);

                // success in ajax does not mean successfully login, so check again
                if (data.status == 'success') {
                    alert(data.message]); // login success message defined in action
                } else {
                    alert(data.message); // login failed message defined in action
                }
            },
            error: function() {
                    // error in ajax means HTTP error, not login error
                    alert('Request failed!');
                }
            });
        });
    });
</script>
相关问题