It is necessary to display the contents of two arrays using ajax. an error occurs in js when parsing an array of Unexpected token {in JSON at position 19

$.ajax({ url: "1.php", type:"POST", success: function(data){ data=JSON.parse(data); console.log( "Прибыли данные: ", data ); } }); <?php $arr1=array("a"=>1,"b"=>2,"c"=>3); echo json_encode($arr1); $arr2=array("foo"=>132,"bar"=>456,4); echo json_encode($arr2); ?> 

    1 answer 1

    Because as soon as you wrote echo once, that's all - the train left. I went to ajax city at the success station (well, or complete). The second echo already going to the beleberdovka.

    To send both, you need to put them in a separate variable:

     $data = ['arr1' => $arr1, 'arr2' => $arr2]; 

    and then send it all:

     echo json_encode($data);