gpt4 book ai didi

php - Javascript JSON.parse : unexpected character error

转载 作者:行者123 更新时间:2023-11-30 10:30:58 26 4
gpt4 key购买 nike

<分区>

我想从 php 脚本中获取一个字符串,并将其解析为一个 javascript 数组。但出现错误:SyntaxError: JSON.parse: unexpected character for the line :JSON.parse(msg);

我找了很多,没弄清楚我的问题在哪里,请帮我检查一下。谢谢。

PHP 方面:

header("application/json; charset=utf-8");
$sum = array(1,2,3,4,5);
echo json_encode($sum);

Javascript:

$.ajax({
type: "POST",
url: "save.php",
contentType: "application/json; charset=utf-8",
data: price,
success: function (msg) {
var i = 0;
console.log(msg);
var sum = new Array();
sum = JSON.parse(msg);
$('input.hj').each(function () {
if (sum[i] > 0) {
$(this).val(sum[i]);
}
i++;
});
}
});

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com