简体   繁体   English

如何从AngularJS的PHP中获取JSON数据

[英]How to get json data from php in angularjs

I already searched lot of stuff but it doesn't help. 我已经搜索了很多东西,但没有帮助。 I want my data from php to be like this : 我希望来自php的数据如下所示:

$scope.places = [{name: 'John'},{name: 'Jane'}];

My problem is I dont know how to achieve this thing. 我的问题是我不知道如何实现这一目标。 Here'e my angularjs looks like: 这是我的angularjs看起来像:

$scope.getNames = function(){
     $http.post('get',{}).then(function(response){
            $scope.places = response.data;
     });
};

$scope.getNames();

PHP 的PHP

$sql = "SELECT * FROM tblplace";
$res = $con->query($sql);
while($row = mysqli_fetch_array($res)){
    // code here.
}

HTML 的HTML

<select class="form-control places">
    <option value="empty">Select</option>
    <option ng-repeat="place in places" value="{{place.name}}">  {{place.name}}</option>
 </select>

How to do it? 怎么做? Thanks! 谢谢!

You need to use json_encode in your php output. 您需要在php输出中使用json_encode。

$json_array = array();
$sql = "SELECT * FROM tblplace";
$res = $con->query($sql);
while($row = mysqli_fetch_array($res)){
   $temp_arr['name'] = $row['name'];
   $json_array[] = $temp_arr;
}
echo json_encode($json_array);

Adjust the while loop accordingly. 相应地调整while循环。 Haven't used php so I'm a bit fuzzy on the syntax. 没用过php,所以语法有点模糊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM