0

i just switched to a mac today and i noticed the JSON.parse that works on other browsers, throws this error on safari.

Unhandled Promise Rejection: SyntaxError: JSON Parse error: Unexpected identifier "undefined"

This is the JSON response from my PHP code

   {"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\"]"}

This is my Javascript code

$.get('chartdata.php', async function (res) {
   console.log(res)
   var res = JSON.parse(res);
});

Please help

UPDATE

I have updated my PHP to echo this

{"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"]}

It works on chrome, safari still throwing this error

Unhandled Promise Rejection: SyntaxError: JSON Parse error: Unexpected identifier "undefined"

Console.log(JSON.parse(res)) shows this on 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]]: Object
Kunle
  • 58
  • 2
  • 9
  • 2
    Why is the array wrapped in quotes? Looks like you are doing too much with json_encode() in the php – charlietfl Feb 05 '22 at 23:28
  • 1
    Backslashes need to be escaped in JSON: `\"` should be `\\"`. – tromgy Feb 05 '22 at 23:54
  • @charlietfl Hello, thank you for your contribution. however, please check the updated question – Kunle Feb 06 '22 at 00:14
  • @tromgy Hello, thanks for your contribution. however, please check my updated question – Kunle Feb 06 '22 at 00:14
  • @tromgy The single backslashes are there to escape the quotes. No need for double backslash – charlietfl Feb 06 '22 at 00:27
  • Try using `$.getJSON()`. What is the error in Safari? – charlietfl Feb 06 '22 at 00:28
  • 1
    Try using a different name for the parse result, e.g instead of `var res = JSON.parse(res);` use `var result = JSON.parse(res);`. Also, why is the function marked `async`? There is no any async code in it. – tromgy Feb 06 '22 at 00:29
  • @tromgy removing async fixed it. thank you so much. i was trying to make the page load faster by using async loading for that script cause while i was testing on my VPS. it was slow, till i moved to my dedicated server, forgot to remove it. thank you guys – Kunle Feb 06 '22 at 00:35

1 Answers1

1

It looks like your PHP code is JSON encoding content that's already JSON encoded, maybe something like this:

$points = ['31 Jan', '01 Feb', '03 Feb'];
echo json_encode(['dataPointsX' => json_encode($points)]);

This yields the oddly quoted string:

{"dataPointsX":"[\"31 Jan\",\"01 Feb\",\"03 Feb\"]"}

You only want to call json_encode() once:

$points = ['31 Jan', '01 Feb', '03 Feb'];
echo json_encode(['dataPointsX' => $points]);
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98