尝试使用CodeIgniter

时间:2016-01-14 01:26:18

标签: ajax codeigniter codeigniter-2

我是CI的新手,我想使用返回到ajax的数据将其用于另一个页面。这是我在控制器中的代码。我成功输出“Hello user”,页面中有额外的字符串。请帮忙!

public function loggedInUser()
    {
        header('Access-Control-Allow-Origin: *');

        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $result = $this->usersdata->verifyLogin($username, $password);

        if($result)
        {
            $user = $result[0]->username;
            $this->session->set_userdata('user', $user);
            $data['user'] = $this->session->userdata('user');
            $data['html'] = $this->load->view('index', $data, TRUE);
            $data['exist'] = TRUE;
            echo json_encode($data);
        }
        else {
            $data['msg'] = "Invalid username or password";
            $data['exist'] = FALSE; 
            echo json_encode($data);
        }
    }

jquery ajax

if(username != "" && password != ""){
                var url = $('#loginForm').attr('action');
                $.ajax({
                    url: url,
                    type: 'POST',
                    dataType: 'json',
                    data: {username: username, password: password},
                    success: function(data) {
                        if (data.exist){
                            window.location.pathname = "trainingexercise.com/login/loggedinuser";
                        }else {
                            $('#dis').html(data.msg);
                        }
                    }
                });
            } 

的index.php

<h3><?php if(!empty($user)) { echo "Hello " . $user . "!"; } ?></h3>
<a href="<?php echo site_url('login/loggedOutUser'); ?>"></a>

请帮助谢谢!

0 个答案:

没有答案