繁体   English   中英

如何从PHP获取返回值到ajax

[英]How to get return value from php to ajax

-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){   
                    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以网格形式获取其可用的访问权限。 如果您还可以向我展示如何在成功的ajax返回数组值中检索到数据后,如何再次使用该数据将其传递回php中,则id将是一个加号。

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);                   
                }
            });
        }
    };
}();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM