dongyu4455 2015-03-31 03:11
浏览 39
已采纳

无法解析来自服务器的codeigniter和soap-JSON数据

Currently I am using in DataTables on server-side in a project. Codeigniter as frame and SOAP as web service.

When I try to run the client-side, a warning was pop out:

JSON data from server could not be parsed

I made my datatable by follow this link.

Here is my server code:

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

This is the result:

JSON:{"sEcho":0,"iTotalRecords":22,"iTotalDisplayRecords":"22","aaData":[]}
  • 写回答

1条回答 默认 最新

  • dtudj42064 2015-03-31 04:19
    关注

    in your looping $rResult->result_array(), you always make new array, and the recent $row array will be replace with new $aRow array. so place your initialization in outside loop statement.

    $row = array();
    $i = 0;
    foreach($rResult->result_array() as $aRow){
        foreach($aColumns as $col){
            $row[$i][$col] = $aRow[$col];
        }
        $i++;
    }
    $output['aaData'] = $row;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败
  • ¥150 计算DC/DC变换器平均模型中的参数mu
  • ¥25 C语言代码,大家帮帮我
  • ¥15 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?
  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)