PHP将数据库对象的数组返回给AJAX Yii2

时间:2017-06-19 15:14:15

标签: php ajax yii2

因此,当我尝试将一个数据库对象数组返回给我的AJAX并解析它时,它就变成了一个字符数组。我的意思是我的json_encode结果是[{'id':38, 'first_name':jana}],当尝试将其解析为ajax中的数组时,发生的是字符数组 - ['[', '{', ''']等。这是我的ajax:

function searchInput() {
    var $content = $('.jobboard-quick-search-form').serialize();
    $.ajax({
        url : '/admin/site/search',
        method : "GET",
        data : $content,
        success : function ( data ) {
            var arr = JSON.parse(data);
            console.log(arr);
        }
    });
}

和我的行动:

public function actionSearch()
    {
        $lang = \frontend\models\Lang::getCurrent();
        $pageSize = 100;

        if(\Yii::$app->request->isAjax)
        {
            $search = \Yii::$app->request->get('search-label');
            $town = \Yii::$app->request->get('towns-list');

            $startsWith = '%'.$search;
            $between = '%'.$search.'%';
            $endsWith = $search.'%';

            $joinDoctors = "SELECT `doctor`.`id`, `doctorLang`.`first_name`, `doctorLang`.`second_name`, `doctorLang`.`city`, `doctorLang`.`hospital_name`
                            FROM `doctor` LEFT JOIN `doctorLang` ON `doctor`.`id`=`doctorLang`.`doc_id`
                            WHERE `doctorLang`.`city`='$town' 
                            AND `doctorLang`.`language`='$lang->url' 
                            AND `doctor`.`active`=1
                            AND (`doctorLang`.`first_name` LIKE '$startsWith'
                            OR `doctorLang`.`first_name` LIKE '$between'
                            OR `doctorLang`.`first_name` LIKE '$endsWith'
                            OR `doctorLang`.`second_name` LIKE '$startsWith'
                            OR `doctorLang`.`second_name` LIKE '$between'
                            OR `doctorLang`.`second_name` LIKE '$endsWith'
                            OR `doctorLang`.`third_name` LIKE '$startsWith'
                            OR `doctorLang`.`third_name` LIKE '$between'
                            OR `doctorLang`.`third_name` LIKE '$endsWith')";

            $doctor =  \Yii::$app->db->createCommand($joinDoctors)->queryAll();


            $joinHospitals = "SELECT `hospital`.`id`, `hospitalLang`.`title`, `hospitalLang`.`address`, `hospitalLang`.`description`
                            FROM `hospital` LEFT JOIN `hospitalLang` ON `hospital`.`id`=`hospitalLang`.`hospital_id`
                            WHERE `hospitalLang`.`city`='$town' 
                            AND `hospitalLang`.`language`='$lang->url' 
                            AND `hospital`.`active`=1
                            AND (`hospitalLang`.`title` LIKE '$startsWith'
                            OR `hospitalLang`.`title` LIKE '$between'
                            OR `hospitalLang`.`title` LIKE '$endsWith')";


            return json_encode($doctor);

        }

    }

提前谢谢!

1 个答案:

答案 0 :(得分:0)

首先添加ajax:

$value = request('webadd');
$value2 = request('notes');

接下来尝试使用restController

dataType: 'json'


$.ajax({
            url:<code>,
            type: <code>,
            dataType: 'json',
            data:<code>
        })