2015-10-27 15:59:58 2698次浏览 0条回答 0 悬赏 100 金钱

view层js代码:

function GetUserInfo(){
    $.ajax({
        url:"http://localhost/index.php?r=site/user-info",
        data:$('#get_ajax_info').serialize(),
        datatype:"json",
        async:false,
        type:'post',
        success:function(data){
            json = $.parseJSON(data);

            if(json.status == 3){
                alert(json.info);
            }else{
                $("#applyinfo-bm_name").val(json.user_name);
                if(json.sex){
                    $("input[type=radio][name=ApplyInfo[sex]][value=1]").attr("checked",'checked');
                }else{
                    $("input[type=radio][name=ApplyInfo[sex]][value=0]").attr("checked",'checked');
                }
                $("#applyinfo-phone").val(json.phone);
                $("#applyinfo-email").val(json.email);
                $("#applyinfo-id_number").val(json.id_card);
            }
        },
        error:function(){
            alert("请重新填写");
        }
    });
}

controller层代码:

    public function actionUserInfo(){
        $model = new Content();
        $id = \Yii::$app->request->post()['user_id'];

        $model->getUserInfo($id);
        //判断是否登录

    }

model层代码:

   public function getUserInfo($id){
        $id = intval($id);

        //判断是否为guest用户
        if($id == 0){
            echo '{"status":0,"info":"请先登录!"}';
        }else{
            $query = new Query();

            $query->select("xingming,sex,phone,email,id_card")
                ->from('user')
                ->where(['id' => $id])->all();
            $command = $query->createCommand();
            $rows = $command->queryOne();

            if($rows['xingming'] == ''){
                echo '{"status":1,"info":"用户名未设置!"}';
            }elseif($rows['id_card'] == ''){
                echo '{"status":2,"info":"身份证号未设置!"}';
            }else{
                $user_info = array();
                $user_info['status'] = 3;
                $user_info['user_name'] = $rows['xingming'];
                if($rows['sex'] == '男'){
                    $user_info['sex'] = 1;
                }else{
                    $user_info['sex'] = 0;
                }
                $user_info['phone'] = $rows['phone'];
                $user_info['email'] = $rows['email'];
                $user_info['id_card'] = $rows['id_card'];
                echo json_encode($user_info);
                //echo '{"status":3,"info":'.$rows['xingming'].'}';
            }

        }

    }

debug查看响应,也能看到数据,但是不知道为什么不可以,刚开始用ajax,求帮忙看下!

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
了然听雨
主管

了然听雨

注册时间:2015-06-08
最后登录:2016-04-21
在线时长:12小时34分
  • 粉丝5
  • 金钱435
  • 威望0
  • 积分555

热门问题