【问题标题】:codeigniter&soap-JSON data from server could not be parsed无法解析来自服务器的 codeigniter&soap-JSON 数据
【发布时间】:2015-03-31 03:11:22
【问题描述】:

目前我在项目服务器端的 DataTables 中使用。 Codeigniter 作为框架,SOAP 作为 Web 服务。

当我尝试运行客户端时,弹出警告:

无法解析来自服务器的 JSON 数据

我按照link 制作了我的数据表。

这是我的服务器代码:

$CI =& get_instance();
        $CI->load->database();


      $aColumns = array('time', 'ip_address', 'player_item','comp_item','result');

        // DB table to use
        $sTable = 'rps';
        //

        $iDisplayStart = $CI->input->get_post('iDisplayStart', true);
        $iDisplayLength = $CI->input->get_post('iDisplayLength', true);
        $iSortCol_0 = $CI->input->get_post('iSortCol_0', true);
        $iSortingCols = $CI->input->get_post('iSortingCols', true);
        $sSearch = $CI->input->get_post('sSearch', true);
        $sEcho = $CI->input->get_post('sEcho', true);

        // Paging
        if(isset($iDisplayStart) && $iDisplayLength != '-1')
        {
            $CI->db->limit($CI->db->escape_str($iDisplayLength), $CI->db->escape_str($iDisplayStart));
        }

        // Ordering
        if(isset($iSortCol_0))
        {
            for($i=0; $i<intval($iSortingCols); $i++)
            {
                $iSortCol = $CI->input->get_post('iSortCol_'.$i, true);
                $bSortable = $CI->input->get_post('bSortable_'.intval($iSortCol), true);
                $sSortDir = $CI->input->get_post('sSortDir_'.$i, true);

                if($bSortable == 'true')
                {
                    $CI->db->order_by($aColumns[intval($CI->db->escape_str($iSortCol))], $CI->db->escape_str($sSortDir));
                }
            }
        }

        /* 
         * Filtering
         * NOTE this does not match the built-in DataTables filtering which does it
         * word by word on any field. It's possible to do here, but concerned about efficiency
         * on very large tables, and MySQL's regex functionality is very limited
         */
        if(isset($sSearch) && !empty($sSearch))
        {
            for($i=0; $i<count($aColumns); $i++)
            {
                $bSearchable = $CI->input->get_post('bSearchable_'.$i, true);

                // Individual column filtering
                if(isset($bSearchable) && $bSearchable == 'true')
                {
                    $CI->db->or_like($aColumns[$i], $CI->db->escape_like_str($sSearch));
                }
            }
        }

        // Select Data
        $CI->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
        $rResult = $CI->db->get($sTable);

        // Data set length after filtering
        $CI->db->select('FOUND_ROWS() AS found_rows');
        $iFilteredTotal = $CI->db->get()->row()->found_rows;

        // Total data set length
        $iTotal = $CI->db->count_all($sTable);

        // Output
        $output = array(
            'sEcho' => intval($sEcho),
            'iTotalRecords' => $iTotal,
            'iTotalDisplayRecords' => $iFilteredTotal,
            'aaData' => array()
        );

        foreach($rResult->result_array() as $aRow)
        {
            $row = array();

            foreach($aColumns as $col)
            {
                $row[] = $aRow[$col];
            }

            $output['aaData'] = $row;
        }

      return json_encode($output);

这是结果:

JSON:{"sEcho":0,"iTotalRecords":22,"iTotalDisplayRecords":"22","aaData":[]}

【问题讨论】:

  • 这是很多要筛选的代码。您是否能够将其本地化为更少的行?

标签: php json codeigniter soap


【解决方案1】:

在你的循环$rResult-&gt;result_array() 中,你总是创建新数组,最近的$row 数组将被新的$aRow 数组替换。所以把你的初始化放在外部循环语句中。

$row = array();
$i = 0;
foreach($rResult->result_array() as $aRow){
    foreach($aColumns as $col){
        $row[$i][$col] = $aRow[$col];
    }
    $i++;
}
$output['aaData'] = $row;

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 2016-04-02
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    相关资源
    最近更新 更多