【问题标题】:How to get json data from php in angularjs如何在angularjs中从php获取json数据
【发布时间】:2017-03-26 03:26:03
【问题描述】:

我已经搜索了很多东西,但没有帮助。我希望我的 php 数据是这样的:

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

我的问题是我不知道如何实现这件事。这是我的 angularjs 看起来像:

$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>

怎么做?谢谢!

【问题讨论】:

    标签: javascript php angularjs json


    【解决方案1】:

    您需要在 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);
    

    相应地调整 while 循环。没用过php所以语法有点模糊。

    【讨论】:

    • 很高兴我能帮上忙。 :D
    猜你喜欢
    • 2016-04-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多