jQuery DataTables SyntaxError: JSON.parse: unexpected character at line X column X of the JSON data When adding a new column
最后我可以开始处理这些数据表。但现在我试图再添加一列,但现在它不再工作了。这是为什么?
它有7根柱子,运转良好。现在有了8,它就不起作用了,我得到了那个错误。
对于7列,这里是整个工作代码:
PHP代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $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 '[cc lang="php"]',print_r($new_array),' |
;$result=array();回声"[";foreach($new_array为$new_array2){回声"[";foreach(新数组2为$value){如果(1==strlen($value))。{$0=0;$value=''.$0.$value.'';}回声$value;如果($value!==结束($new_array2))//参考:http://stackoverflow.com/a/8780881/1883256回声',';}}echo']';//referencias:http://www.mydigitallife.info/how-to-access-php-array-and-multipdimension-nested-array-code-syntax/如果($new阵列2!==结束($new_array))。{回声',';}其他回音'';}回声"]";
1 | <P>这是7列PHP脚本输出数组:</P>[cc lang="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代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | $(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数据表代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <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列时,如下所示:
1 | [[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:json数据的第1行第33列出现意外字符
当然,我在HTML代码中添加了一列。
我不知道虫子在哪里。我该怎么解决这个问题?
就在第33个字符(在第二个数据集中)之前,您有一个额外的逗号,这里是:
1 | [[2753,"08",16,21,39,50,52,"04", <<< |