0

我正在从我的视图中进行一个简单的 ajax 调用:

<script type="text/javascript">
    function verifyLogin(){
        var data = null;

        var email = document.getElementsByName( "email" )[0].value;
        var pwd = document.getElementsByName( "password" )[0].value;  

        $.ajax({
            type : 'POST',           
            url : server_url + 'application/login', // Servlet URL           
            data:{
                'email':email,
                'pwd':pwd
            },
            timeout: 10000,
            dataType: 'jsonp',
            success : function(data) {
                if(data.logged_in){
                    //We set up the Secuity Key
                        //FOR BROWSER TESTING ONLY !!!!!!!!!!!!!
                    setCookie("key", data.user.key, 1);

                    //Redirect
                    window.location.href="home.php";

                } else {
                    alert("Invalid Login!!");
                        console.log( data );
                    if( data.errors ) {
                        //define
                        var error = {};
                        error.alert = data.errors;

                        //Append
                        var template = Handlebars.compile( $('#alertTemplate').html() );
                        $('#errors').empty().append( template(error) );

                        //Erase
                        error = {};
                    }
                }
            },
            error : function( xhr, type )
            {
                alert('server error occurred');
            } 
        });
    }
</script>

我在 CodeIgniter 中以 followfin 方法接收它:

public function login()
    {
        //Gestion du Formulaire
        $this->load->library( 'form_validation' );
        $this->form_validation->set_rules( 'email', 'email', 'trim|required|valid_email|callback__check_login' );
        $this->form_validation->set_rules( 'pwd', 'password', 'trim|required' );

        if( $this->form_validation->run() )
        {
        // the form has successfully validated
            $email = strtolower( $this->input->post( 'email' ) );

            //Use Doctrine to retrieve user by Pwd
            $em = $this->doctrine->em;
            $pwd = hash( 'sha256', $this->input->post( 'pwd' ) . $email );
            $user = $em->getRepository( 'Entity\User' )->findOneBy( array( 'email' => $email, 'password' => $pwd ) );

            if( $user )
            {
                //Everything is Fine !
                    //We generate a key
                $key = $this->key->create(10, 1);

                //We retrieve the entity freshly generated
                $key_entity = $em->getRepository( 'Entity\Security_Key' )->findOneBy( array( 'value' => $key ) );

                //We associate it to the user:
                $user->setPrivateKey( $key_entity );

                // We can now persist this entity:
                try
                {
                    $em->persist($user);
                    $em->flush();
                }
                catch(\PDOException $e)
                {
                    // Error When Persisting the Entity !!
                    $array = array(
                                   'errors' => "<p>Server Error</p>",
                                   'logged_in' => FALSE
                               );

                    $this->output
                         ->set_content_type( 'application/json' )
                         ->set_output( json_encode( $array ) );

                    return FALSE;
                }

                // End persisting entity / attach key-user


                // Everything is fine, send the data back ! 
                $user_array = array(
                                    'id' => $user->getId(),
                                    'name' => $user->getUsername(),
                                    'key' => $key
                                );

                $array = array(
                               'user' => $user_array,
                               'logged_in' => TRUE
                           );

                $this->output
                     ->set_content_type( 'application/json' )
                     ->set_output( json_encode( $array ) );

                return TRUE;
            }

            //We didn't fin any matches
            $this->output
                 ->set_content_type( 'application/json' )
                 ->set_output( json_encode( array(  'errors' => "<p>Wrong password / email combination</p>",
                                                    'logged_in' => FALSE 
                                                ) ) );
            return FALSE;

        }

        //Error in the Form validation
        $this->output
             ->set_content_type( 'application/json' )
             ->set_output( json_encode( array(  'errors' => validation_errors(),
                                                'logged_in' => FALSE 
                                              ) ) );
        return FALSE;
    }

我究竟做错了什么 ?我应该以特定方式配置 codeigniter 吗?它与 json 一起工作得很好吗?

我应该在视图或标题中更改某些内容吗?

谢谢

4

0 回答 0