简体   繁体   中英

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 :

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

My problem is I dont know how to achieve this thing. Here'e my angularjs looks like:

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

$scope.getNames();

PHP

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

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.

$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. Haven't used php so I'm a bit fuzzy on the syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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