2015-11-26 19:14:55 2826次浏览 0条回答 0 悬赏 50 金钱

控制器代码:

namespace frontend\controllers;
use ....;

class RegisterController extends \core\frontend\FrontendController
{
    ......
    public function actionIndex()
    {
        .......
        if(Yii::$app->request->post())
        {
             //添加账号用户数据
             $model->da_realname = Tkx::getCookie('nickname');
             $model->save();
             ......     
        }else{
            return $this->render('index',[
                'model'=>$model,
            ]);
        }
    }


    public  function actionMessage(){
        if(Yii::$app->request->get()){
            ......
            if($res){
                $data = array(
                    'status'=>'1'
                );
            }else{
                $data = array(
                    'status'=>'0'
                );
            }
            echo  json_encode($data);
        }else{
            Tkx::wxstate('访问非法');
        }
    }
}

ajax请求

 $.get("<?=\Yii::$app->urlManager->createUrl(['register/message']).'&phone_num=';?>"+phone_num,{},function(data){
                    if(data.status=='1'){
                        setCookie('code','code');
                        alert('验证码发送成功');
                    }else{
                        alert('验证码发送失败');
                    }
                },'json');

核心类操作COOKIE的方法:

    public  static  function  getCookie($key){
        $cookies = Yii::$app->response->cookies;
        return $cookies->getValue($key,false);
    }

问题:当getCookie方法使用response的时候,jquery发起的ajax函数的内容无效,反而是直接打印出了本应返回的JSON字符串;当getCookie方法改成request的时候,一切正常

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
jhomephper
副总裁

jhomephper 帝都

注册时间:2015-05-05
最后登录:2021-04-20
在线时长:78小时1分
  • 粉丝32
  • 金钱5330
  • 威望10
  • 积分6210

热门问题