【发布时间】:2017-10-04 21:29:52
【问题描述】:
我是 Angularjs 和 SlimPHP 的新手,我在将数据解析为 json 时遇到了一些麻烦。我刚刚启动了一个新应用程序,但我不知道为什么它不起作用。
这是我的控制器的功能,它似乎正在工作(我可以使用 print_r 来显示 $poles),但不能返回 Json。在 Firefox 网络中,我有错误:“SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data”。
public function getPoles($request, $response) {
$poles = $this->container['form.dao']->getPoles();
if ($poles == null) {
return json_encode(["error" => "no data found"]);
}
return $response->withJson($poles, 200);
}
formDAO.php 中的 getPoles() 函数:
public function getPoles() {
$request = "SELECT * FROM menu_pole ORDER BY id";
try {
$stmt = $this->db->query($request);
$poles = $stmt->fetchAll(\PDO::FETCH_OBJ);
return $poles;
} catch(\PDOException $e) {
return '{"error":{"text":'. $e->getMessage() .'}}';
}
}
我想我可能遗漏了一些明显的东西。
编辑:
使用 print_r,我得到了这个:
Array(
[0] => stdClass Object
(
[id] => 1
[libelle] => GE
)
[1] => stdClass Object
(
[id] => 2
[libelle] => GP
)
[2] => stdClass Object
(
[id] => 3
[libelle] => GS
)
[3] => stdClass Object
(
[id] => 4
[libelle] => NO
)
[4] => stdClass Object
(
[id] => 5
[libelle] => DH
)
[5] => stdClass Object
(
[id] => 6
[libelle] => CRC
)
[6] => stdClass Object
(
[id] => 7
[libelle] => SG
))
【问题讨论】: