我有一个响应数组的API。我想列出ng-repeat。
这是php Api代码
connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT id, First_Name, Last_time,Email,Phone_Number,Location,Phd_Degree,Phd_University,MS_Degree,MS_University,BS_Degree,BS_University FROM user"; $result = $conn->query($sql); $data=array(); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $data[]=array( 'id'=>$row['id'], 'First_Name'=>$row['First_Name'], 'Last_time'=>$row['Last_time'], 'Email'=>$row['Email'], 'Phone_Number'=>$row['Phone_Number'], 'Location'=>$row['Location'], 'Phd_Degree'=>$row['Phd_Degree'], 'Phd_University'=>$row['Phd_University'], 'MS_Degree'=>$row['MS_Degree'], 'MS_University'=>$row['MS_University'], 'BS_Degree'=>$row['BS_Degree'], 'BS_University'=>$row['BS_University'] ); //echo "id: " . $row["id"]. " - Name: " . $row["First_Name"]. " " . $row["Last_time"]. "
"; } } else { $data[]=array( 'message'=>'not success', ); } print_r($data); //return $dat=json_encode($data); $conn->close(); ?>
这是我的角度代码。
app.controller('listuser', function ($scope,$http,$log) { $scope.data=[]; return $http({ method: 'POST', url: 'apisource.php', }) .then(function (results) { $scope.data=results.data; $log.log($scope.data); }); });
现在的问题是当我试图在html上实现它无法正常工作时
这是html代码
{{x.First_Name}}
我认为问题在于我的回答,因为它不是Json格式,但是当我尝试做json_encode()时,然后在响应中它显示为空白。
以下是我的控制台中显示的响应。
Array ( [0] => Array ( [id] => 1 [First_Name] => Zachary [Last_time] => Chatha [Email] => kouda@gmail.com [Phone_Number] => 7745752121 [Location] => Guden�vej 1, 20 Vanl�se, Denmark, Apt 2 [Phd_Degree] => test [Phd_University] => test [MS_Degree] => Master of Science in Pharmaceutical Sciences (MSPS) [MS_University] => Copenhagen University (Denmark) [BS_Degree] => \nBachelor of Science, Biological Applications and Technologies\n [BS_University] => University of Ioannina (Greece) ) [1] => Array ( [id] => 2 [First_Name] => Gaurav [Last_time] => Kumar [Email] => gtest@gmail.com [Phone_Number] => 7745752121 [Location] => Guden�vej 1, 20 Vanl�se, Denmark, Apt 2 [Phd_Degree] => test [Phd_University] => test [MS_Degree] => Master of Science in Pharmaceutical Sciences (MSPS) [MS_University] => Copenhagen University (Denmark) [BS_Degree] => \nBachelor of Science, Biological Applications and Technologies\n [BS_University] => University of Ioannina (Greece) ) )
答案 0 :(得分:0)
在带有json标头的php打印中,而不是返回:
header('Content-Type: application/json');
echo json_encode($data);