gpt4 book ai didi

php - 使用 xmlhttp.responseText 中的 JSON 解析数组

转载 作者:行者123 更新时间:2023-12-02 18:28:57 25 4
gpt4 key购买 nike

我尝试使用ajax将php数组获取到js,并在PHP中通过JSON编码并在JS中解码,但无法获得所需的结果。

我在回显编码字符串之前使用 var_dump,这是输出:

$boardArrayEncoded = json_encode($boardArray);
var_dump($boardArrayEncoded);
echo $boardArrayEncoded;

JSON_encode 后的字符串是:

string '{"1x1":0,"1x2":0,"1x3":0,"1x4":0,"1x5":0,"1x6":0,"2x1":0,"2x2":0,"2x3":0,"2x4":0,"2x5":0,"2x6":0,"3x1":0,"3x2":0,"3x3":0,"3x4":0,"3x5":0,"3x6":0,"4x1":0,"4x2":0,"4x3":0,"4x4":0,"4x5":0,"4x6":0,"5x1":0,"5x2":0,"5x3":0,"5x4":0,"5x5":0,"5x6":0,"6x1":0,"6x2":0,"6x3":0,"6x4":0,"6x5":0,"6x6":0}'

在 javascript 中我尝试解析它:

if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("logDiv1").innerHTML = xmlhttp.responseText;
array = JSON.parse(xmlhttp.responseText);
}

这是 xmlhttp.responseText:{"1x1":0,"1x2":0,"1x3":0,"1x4":0,"1x5":0,"1x6":0, "2x1":0,"2x2":0,"2x3":0,"2x4":0,"2x5":0,"2x6":0,"3x1":0,"3x2":0,"3x3 ":0,"3x4":0,"3x5":0,"3x6":0,"4x1":0,"4x2":0,"4x3":0,"4x4":0,"4x5": 0,"4x6":0,"5x1":0,"5x2":0,"5x3":0,"5x4":0,"5x5":0,"5x6":0,"6x1":0, "6x2":0,"6x3":0,"6x4":0,"6x5":0,"6x6":0}

在 Firebug 上我收到语法错误:

SyntaxError: JSON.parse: unexpected character
[Break On This Error]

array1 = JSON.parse(xmlhttp.responseText);

我做错了什么?我需要在 JS 中使用这个数组,如何正确编码?

提前致谢

最佳答案

删除 var_dump。它是打印输出,这可能导致您所说的responseText不是完整的responseText。

我希望您的回复文本是:

string '{"1x1":0,"1x2":0,"1x3":0,"1x4":0,"1x5":0,"1x6":0,"2x1":0,"2x2":0,"2x3":0,"2x4":0,"2x5":0,"2x6":0,"3x1":0,"3x2":0,"3x3":0,"3x4":0,"3x5":0,"3x6":0,"4x1":0,"4x2":0,"4x3":0,"4x4":0,"4x5":0,"4x6":0,"5x1":0,"5x2":0,"5x3":0,"5x4":0,"5x5":0,"5x6":0,"6x1":0,"6x2":0,"6x3":0,"6x4":0,"6x5":0,"6x6":0}'
{"1x1":0,"1x2":0,"1x3":0,"1x4":0,"1x5":0,"1x6":0,"2x1":0,"2x2":0,"2x3":0,"2x4":0,"2x5":0,"2x6":0,"3x1":0,"3x2":0,"3x3":0,"3x4":0,"3x5":0,"3x6":0,"4x1":0,"4x2":0,"4x3":0,"4x4":0,"4x5":0,"4x6":0,"5x1":0,"5x2":0,"5x3":0,"5x4":0,"5x5":0,"5x6":0,"6x1":0,"6x2":0,"6x3":0,"6x4":0,"6x5":0,"6x6":0}

关于php - 使用 xmlhttp.responseText 中的 JSON 解析数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18032575/

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