我在mysql中有数据我正在使用url编码转换为json,
我正在使用angularjs来获取数据这是我的下面angularjs代码
这是我的角度脚本:
angular
.module('hameedApp', [])
.controller('hameedController', function($scope, $http) {
$http({
method: 'GET',
url: 'json2.php'
}).then(function(response) {
$scope.contacts = response.data;
}).catch(function(response) {
console.log('error');
})
});
这是我的json2.php代码:
<?php
$connect = mysqli_connect("localhost", "root", "", "recruiter");
$sql = "SELECT * FROM recruiter";
$result = mysqli_query($connect, $sql);
$json_array = array();
while($row = mysqli_fetch_assoc($result))
{
$json_array[] = $row;
}
echo print_r(json_encode($json_array));
?>
如果我在postman url中击中json2.php
,成功地得到了json的响应,
如果我把{{contacts}}
放在我的html文件中,成功我得到了json响应,但如果我使用ng-repeat {{contact.somestuff}}
,我没有得到任何数据。有任何想法吗 ?
有解决方案。我刚刚删除了print_r
及其作品!
<?php
$connect = mysqli_connect("localhost", "root", "", "recruiter");
$sql = "SELECT * FROM recruiter";
$result = mysqli_query($connect, $sql);
$json_array = array();
while($row = mysqli_fetch_assoc($result))
{
$json_array[] = $row;
}
echo (json_encode($json_array));
?>