2017-06-19 51 views
0

因此,當我嘗試將數據庫對象的數組返回給我的AJAX並解析它時,它變成了一個字符數組。我的意思是我json_encode結果是[{'id':38, 'first_name':jana}]當嘗試將其解析到在AJAX是什麼發生陣列和字符數組 - ['[', '{', ''']等,這是我的ajax:PHP將數據庫對象的數組返回給AJAX Yii2

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); 

     } 

    } 

預先感謝您!所有的

+0

你要拋棄你所需要的值數都 – scaisEdge

+0

首先 - 你的代碼是開放的SQL注入,使用[Yii 2查詢生成器](http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html)。其次 - 您是否嘗試刪除'JSON.parse(data);'並直接輸出'data'?很有可能它已經解析了JSON。 – Bizley

+0

是的,我沒有。 –

回答

0

首先加入AJAX:

dataType: 'json' 


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

下一頁嘗試使用restController

class DataController extends yii\rest\Controller 
{ 
    public function actionSearch() 
    { 
     [.....] 
     return \Yii::$app->db->createCommand($joinDoctors)->queryAll(); 
    } 
}