ty532021 2016-12-01 13:46:54 3397次浏览 0条回复 1 0 0

以下是我在项目中出现的一个问题: 功能:用户通过AJAX登录,与后台进行数据交互。判断是否有这个用户,没有则提示用户去注册,有则判断密码的正确性。 使用AJAX提交数据,报错,然后提示性“YUnable to verify your data submission.” 以下是我使用的解决办法。欢迎大家指正。 VIEW层代码层

<?= Html::csrfMetaTags() ?><!--header部分加入-->
<div class="container">
			<div class="signup">
                                <?php
                                echo Html::textInput('username','',['class'=>'form-control phone','placeholder'=>'手机号','id'=>'signupname']);
                                ?>
                                <?php
                                echo Html::textInput('work','',['class'=>'form-control lock','placeholder'=>'请输入您的姓名','id'=>'work']);
                                ?>
                                 <?php
                                echo Html::passwordInput('password','',['class'=>'form-control lock','placeholder'=>'为您的账号设置一个密码吧','id'=>'signuppass']);
                                ?>
                                <?php
                                echo Html::submitInput('登录', ['class'=>'btn btn-default sign-btn']);
                                ?>
                            <p class="login_signup"><a href="<?php echo Url::toRoute(['index/login'])?>" id="login">已有账号?立即登陆>></a></p>
			</div>
			
		</div>

JS部分

$.ajax({
                                        url:"<?php echo Url::toRoute(['index/login'])?>",
                                        type:'POST',
                                        dataType:'json',
                                        data:{"username":$("#loginusername").val(),"password":$("#loginpassword").val(),'csrf':$('meta[name="csrf-token"]').attr("content")},
                                        success:function(data){
                                            if(data.code==001){
                                                show(data.message);
                                                return false;
                                            }
                                            if(data.code==002){
                                                show(data.message);
                                                return false;
                                            }
                                            if(data.code==003){
                                                location.href="<?php echo Url::toRoute(['manager/index'])?>";
                                            }
                                        }
                                     });

Controller控制器

public function beforeAction($action) 
    { 
        $this->enableCsrfValidation = false; 
        return parent::beforeAction($action); 
    }

public function actionLogin()
    {
        $this->layout = false;
        if(Yii::$app->request->isAjax){
            $cusers = new Cusers();
            $post = Yii::$app->request->post();
            if(!$cusers->login($post)){
               echo json_encode(['code'=>003]);
               exit();
            }
        }
        return $this->render('login');
    }

Model层

public function rules() {
        return [
            ['username','validateUser','on'=>'login'],
            ['password','validatePass','on'=>'login'],
        ];
    }


public function validateUser()
    {
        if(!$this->hasErrors()){
            $userInfo = $this->find()->where('username =:name',[':name'=>  $this->username])->one();
        }
        if(is_null($userInfo)){
            echo json_encode(['code'=>001,'message'=>'账号没有注册,请进行注册!']);
            exit();
        }
    }
    


    public function validatePass()
    {
        if(!$this->hasErrors()){
            $userInfo = $this->find()->where('username =:name',[':name'=>  $this->username])->one();
        }
        if($userInfo->password !== md5($this->password)){
            echo json_encode(['code'=>002,'message'=>'账号或者密码错误']);
            exit();
        }
    }
public function login($data){
        $this->scenario = 'login';
        if($this->load($data,'') && $this->validate()){
                $userInfo = $this->find()->where('username =:name',[':name'=>$this->username])->one();
                $cookies = Yii::$app->response->cookies;
                $cookies->add(new \yii\web\Cookie([
                    'name'=>'username',
                    'value'=>$this->username,
                    'expire'=>  time()+86400,
                ]));
                $cookies->add(new \yii\web\Cookie([
                         'name'=>'userid',
                         'value'=>$userInfo->id,
                         'expire'=>  time()+86400,
                ]));
                Yii::$app->response->send();
                echo json_encode(['code'=>003]);
                exit(); 
        }
        return false;
    }
    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册