Acabo de cambiar a una Mac hoy y noté que JSON.parse que funciona en otros navegadores arroja este error en Safari.
Unhandled Promise Rejection: SyntaxError: JSON Parse error: Unexpected identifier "undefined"Esta es la respuesta JSON de mi código PHP
{"dataPointsX": "[\"31 Jan\",\"01 Feb\",\"02 Feb\",\"03 Feb\",\"04 Feb\",\"05 Feb\",\"06 Feb\"]", "dataPointsY": "[\"0\",\"0\",\"7287\",\"24572\",\"30657\",\"27865\",\"0\"]", "dataPoints2Y": "[\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]"}Este es mi código Javascript
$.get('chartdata.php', async function (res) { console.log(res) var res = JSON.parse(res); });Por favor ayuda
ACTUALIZAR
He actualizado mi PHP para hacer eco de esto
{"dataPointsX":["31 Jan","01 Feb","02 Feb","03 Feb","04 Feb","05 Feb","06 Feb"],"dataPointsY":["0","0","7287","24572","30632","27820","0"],"dataPoints2Y":["0","0","0","0","0","0","0"]}Funciona en Chrome, Safari sigue arrojando este error.
Unhandled Promise Rejection: SyntaxError: JSON Parse error: Unexpected identifier "undefined"Console.log(JSON.parse(res)) muestra esto en Chrome
dataPoints2Y: (7) ['0', '0', '0', '0', '0', '0', '0'] dataPointsX: (7) ['31 Jan', '01 Feb', '02 Feb', '03 Feb', '04 Feb', '05 Feb', '06 Feb'] dataPointsY: (7) ['0', '0', '7287', '24572', '30489', '27744', '0'] [[Prototype]]: ObjectParece que su código PHP es contenido de codificación JSON que ya está codificado en JSON, tal vez algo como esto:
$points = ['31 Jan', '01 Feb', '03 Feb']; echo json_encode(['dataPointsX' => json_encode($points)]);Esto produce la cadena entre comillas extrañas:
{"dataPointsX":"[\"31 Jan\",\"01 Feb\",\"03 Feb\"]"}Solo desea llamar a json_encode() una vez:
$points = ['31 Jan', '01 Feb', '03 Feb']; echo json_encode(['dataPointsX' => $points]);