jquery - PHP generates invalid JSON -


i have following json string i'm passing php file via echo when called html file via jquery ajax call.

the string contains html fed datatables cell. returned json appears interpreting </ line feed it's inserting ascii 10 , 13 (per text ascii conversion website), causing datatables see invalid json.

i'm unclear generating misplaced ascii.

html (datatables load):

var mydatatable = $("#myassets").datatable( {     "ajax": "../php/fetchmydata.php",     "type": "post",     "datatype": "json",     "columns": [         {"data":"teamname","width":"30%"},         {"data":"teamimg","width":"20%"},                    {"data":"cluesfoundcount","width":"5%"},         {"data":"assets","width":"30%"},         {"data":"scores","width":"5%"}     ] });//end datatable 

php (fetchmydata.php):

$myjson = '{"teamname":"<div class=\'teamblock\'><span class=\'bold\'>' . $row['teamname'] . '</span><div>rick</div></div>"'; $myjson.='}'; echo '{"data":[' . $myjson . ']}';  return $myjson; 

you need use json_encode avoid errors.

use following code instead:

$data = array(    "data" => array(       array(          "teamname" =>             '<div class="teamblock"><span class="bold">' .              $row['teamname'] .              '</span><div>rick</div></div>',          "teamimg" => "",          "cluesfoundcount" => "",          "assets" => "",          "scores" => ""       )    ) );  echo json_encode($data); 

however avoid passing lot of data via ajax, it's recommended produce html on client-side using columns.render option.


Comments