2014-05-19 64 views
0

最後,我可以使用這些DataTable。但是現在我試着添加一列,現在不再工作了。爲什麼是這樣?jQuery DataTables SyntaxError:JSON.parse:JSON數據第X行的意外字符添加新列時

與7列它工作正常。現在用8,它只是沒有工作,我得到的錯誤。

對於這裏7列是整個工作的代碼:

PHP代碼:

$loteria='7column';//it doesn't work for '8columns' 
$lotto = new Lotto(); 
$ultimos_resultados=$lotto->last_results($loteria,$sorteos); 

function objectToArray($d) 
{ 
    if (is_object($d)) { 
     $d = get_object_vars($d); 
    } 

    if (is_array($d)) { 
     return array_map(__FUNCTION__, $d); 
    } else { 
     // Return array 
     return $d; 
    } 
} 

$new_array = objectToArray($ultimos_resultados); 
//echo '<pre>',print_r($new_array),'</pre>'; 

$result = array(); 
echo '['; 
foreach ($new_array as $new_array2) { 
    echo '['; 
    foreach ($new_array2 AS $value){ 
     if (1 == strlen($value)) { 
      $zero=0; 
      $value = '"'.$zero.$value.'"'; 
     } 
     echo $value; 
     if($value!==end($new_array2)){ //referencias: http://stackoverflow.com/a/8780881/1883256 
      echo','; 
     } 
    } 
    echo ']';//referencias: http://www.mydigitallife.info/how-to-access-php-array-and-multidimensional-nested-arrays-code-syntax/ 
    if($new_array2!==end($new_array)){ 
     echo ','; 
    }else{ echo '';} 
} 
echo ']'; 

這是7列PHP腳本輸出數組:

[[2753,17,26,28,31,37,47],[2754,"08",10,23,26,44,56],[2755,"04",12,16,20,22,47],[2756,12,19,33,34,41,55],[2757,32,34,35,36,50,55]] 

jQuery代碼:

$(document).ready(function() { 

$.ajax({ 
    url: "ajax/default_chart_numbers_table.php", 
    type: "post", 
    data: {loteria: '7-column', sorteos: 5}, /*Number of rows*/ 
    success : function (resp){ 
     // would look something like ['val1','val2', 'etc'] 
     var column_data = $.parseJSON(resp); 
     alert(column_data); 
     //alert('The element of array number [2] is:' + column_data[2]);   
     // adding data to datatables 
     // if column_data is 1 row 
     //$('#dataTables-melate').dataTable().fnAddData(column_data); 
     for (var j=0;j<=column_data.length-1;j++){ 
      // adding each row with its column data 
      //iterating values to add leading zeros to single digits:    
      $('#dataTables-melate').dataTable().fnAddData(column_data[j]); 
     } 
    }, 
    error: function(jqXHR, textStatus, ex) { 
     console.log(textStatus + "," + ex + "," + jqXHR.responseText); 
    } 
});  

}); 

HTML代碼數據表:

<table id="dataTables-melate" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%"> 
           <thead> 
            <tr> 
             <th>Concurso</th> 
             <th>R1</th> 
             <th>R2</th> 
             <th>R3</th> 
             <th>R4</th> 
             <th>R5</th> 
             <th>R6</th> 
             <!--<th>R7</th> THIS IS FOR 8th column--> 
            </tr> 
           </thead> 

           <tfoot> 
            <tr> 
             <th>Concurso</th> 
             <th>R1</th> 
             <th>R2</th> 
             <th>R3</th> 
             <th>R4</th> 
             <th>R5</th> 
             <th>R6</th> 
             <!--<th>R7</th> THIS IS FOR 8th column--> 
            </tr> 
           </tfoot> 
          </table> 

直到這裏一切都很好......但是當PHP腳本輸出數組有8個欄目是這樣的:

[[2753,"08",16,21,39,50,52,"04",],[2754,11,18,31,35,39,42,34],[2755,"04",20,29,31,44,48,49],[2756,"05","06",33,34,46,55,38],[2757,"06",18,36,48,50,52,28]] 

那麼它不再工作,在這裏我得到的以下錯誤:

語法錯誤:JSON.parse:位於第1行的JSON數據的33列意外的字符

當然,我在HTML代碼中再添加一列。

我不知道錯誤在哪裏。我該如何解決?

回答

2

就在第33個字符(第二數據集)你有一個額外的逗號,在這裏:

[[2753,"08",16,21,39,50,52,"04", <<< 
+0

事實上!這就對了!!!謝謝!!! – Pathros

相關問題