这个问题在这里已有答案:
我想从我的数据库中获取数据到我的Angular.js。
所以我创建了一个php文件,用于选择我的数据。我读到我必须使用jsonp,否则我将无法获取数据。
data.php
<?php
$db = mysqli_connect("xxx", "xxx", "xxx", "xxx");
$select = "select * from product;";
$result = mysqli_query($db,$select);
if ($result) {
$i=0;
$outp = "";
while ($row = mysqli_fetch_assoc($result))
{
if ($outp != "") {$outp .= ",";}
$outp .= '"preis":"' . $row["preis"] . '",';
$outp .= '"name":"'. $row["name"] . '"}';
}
$outp ='{"records":['.$outp.']}';
echo($outp);
}
?>
App.js
var url = "http://server/data/data.php?callback=JSON_CALLBACK"
$http.jsonp(url)
.success(function(data){
console.log(data.found);
});
现在我收到语法错误:“SyntaxError:unexpected token:':'”
谢谢你的帮助
不要自己构建JSON。试试这个。主要的是,只需按照您喜欢的方式创建一个数组,然后使用json_encode()
$result = [];
while ($row = mysqli_fetch_assoc($result))
{
$result[] = $row;
}
echo json_encode($result);