1

我的 jquery 脚本无法获得有效响应。

我的回复:

{
    "success": true,
    "data": {
        "attack": null,
        "shield": null,
        "speed": null,
        "cargo": null,
        "upg_slots": "1"
    }
}

代码:

echo json_encode(array(
    'success' => true,
    'data' => array(
        'attack' => null,
        'shield' => null,
        'speed' => null,
        'cargo' => null,
        'upg_slots' => "1"
    )
));

有效回复:

{
    "success": true,
    "data": [
        {
            "attack": null,
            "shield": null,
            "speed": null,
            "cargo": null,
            "upg_slots": "1"
        }
    ]
}

提前致谢!

4

2 回答 2

1

看起来您'data'在响应中的键下又缺少了一个数组,只需像这样再次包装它:

 echo json_encode(array('success' => true, 'data' => array(array( 'attack' => null, 'shield' => null, 'speed' => null, 'cargo' => null, 'upg_slots' => "1"))));
//                                                    ^^^
于 2013-02-18T21:38:49.810 回答
1

如果您需要一个包含单个字典的列表,则需要将关键字数组嵌套在位置数组中:

$data = array(
    array(
        'attack' => null,
        'shield' => null,
        'speed' => null,
        'cargo' => null,
        'upg_slots' => "1"
    )
);
echo json_encode(array('success' => true, 'data' => $data));
于 2013-02-18T21:39:40.653 回答