如何从php到ajax获得返回值


How to get return value from php to ajax

—Ajax Code

   var User = function(){
    return {
        init : function(){
            document.getElementById('login').addEventListener('click', this.login);
        },
        login : function(){
            var username = $("#username").val(),
                password = $("#password").val();
            $.ajax({
                url : 'http://localhost/oc2/user_info/login',
                method : 'post',
                dataType : 'json',
                data : {
                    username : username,
                    password : password
                },
                success : function(response){   
                    alert('h'); <-- add the php return value id here.
                    window.location.href = "main.html";
                },
                error : function(response){
                    alert(response.responseText);                   
                }
            });
        }
    };
}();

——PHP CodeIgniter

public function login()
    {
        $post = $this->input->post();
        $where = array(
            'email_address' => $post['username'], //"turbobpo.johnrey@gmail.com", 
            'password' => md5($post['password']) //"e10adc3949ba59abbe56e057f20f883e" 
        );
        $user_info = $this->get_by($where);

        if(isset($user_info['id']))
        {
            $this->session->set_userdata('user_info', $user_info);
            $response = array(
                'id' => $user_info['id'], <-- this i want to pass to my ajax
                'success' => TRUE
            );
        }
        else
        {
            $response = array(
                'success' => FALSE
            );
        }
        print json_encode($response);
    }

你好,你可以帮助我这部分我已经管理走得很远在这个php ajax我不使用创建这个应用程序请我需要帮助我在代码上放置一个评论,看看我想从php检索值到我的ajax代码,所以我可以使用它在我的下一个检索文件,我使用登录用户的id,以获得他的可用访问在网格形式。如果您还能告诉我如何使用该数据在PHP中再次传递id在检索成功后ajax返回数组值

var User = function(){
    return {
        init : function(){
            document.getElementById('login').addEventListener('click', this.login);
        },
        login : function(){
            var username = $("#username").val(),
                password = $("#password").val();
            $.ajax({
                url : 'http://localhost/oc2/user_info/login',
                method : 'post',
                dataType : 'json',
                data : {
                    username : username,
                    password : password
                },
                success : function(response){   
                    response.id; // Here is the id JQuery parses JSON for you
                    window.location.href = "main.html";
                },
                error : function(response){
                    alert(response.responseText);                   
                }
            });
        }
    };
}();