我今天刚切换到mac,我注意到在其他浏览器上运行的JSON.parse在safari上抛出了这个错误。
Unhandled Promise Rejection: SyntaxError: JSON Parse error: Unexpected identifier "undefined"
这是我的PHP代码的JSON响应
{"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"]"}
这是我的Javascript代码
$.get('chartdata.php', async function (res) {
console.log(res)
var res = JSON.parse(res);
});
请帮助
更新
我已经更新了我的PHP以响应这个
{"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"]}
它在chrome上工作,safari仍然抛出这个错误
Unhandled Promise Rejection: SyntaxError: JSON Parse error: Unexpected identifier "undefined"
Console.log(JSON.parse(res((在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
看起来你的PHP代码是JSON编码的内容,可能是这样的:
$points = ['31 Jan', '01 Feb', '03 Feb'];
echo json_encode(['dataPointsX' => json_encode($points)]);
这就产生了一个奇怪的引号字符串:
{"dataPointsX":"["31 Jan","01 Feb","03 Feb"]"}
您只想调用json_encode((一次:
$points = ['31 Jan', '01 Feb', '03 Feb'];
echo json_encode(['dataPointsX' => $points]);