AngularJS不会显示编辑的MySQL结果*

时间:2016-05-23 12:48:48

标签: javascript php jquery mysql angularjs

MySQL数据的概述(root.html)视图不显示数据。但是打开/php/overview.php表示可以正确获取数据:

  

成功连接MySQL服务器。 [{" JOB_TITLE":"闪速"" JOB_ID":" doggg"},{" JOB_TITLE":& #34; AD34"" JOB_ID":" teeee"}]

root.html

<table class="setup-table">
    <tr>
        <td>Job title</td>
        <td>Job ID</td>
    </tr>
    <tr data-ng-repeat="campaign in campaigns">
        <td>A {{campaign.Job_title}}</td>
        <td>B {{campaign.Job_id}}</td>

    </tr>
</table>

controller.js(注意警告&#34;它工作(稍后删除)&#34;显示,但视图中的表是空的:

ctr.controller
('RootCtrl', ['$scope', '$routeParams', '$http', function($scope, $routeParams, $http)

                {

                    $http.get("php/overview.php")
                        .then(function successCallback(response){
                            console.log(response);
                            $scope.campaigns = response.data;
                            alert("It WORKED (DELETE ME)");

                        }
                        , function errorCallback(response) {
                            $scope.campaigns = "error in fetching data";
                            alert("DID NOT WORK ");}
                            )
                        ;}

                ]);

overview.php

<?php
include_once('db.php');

$query = "SELECT Job_title, Job_id FROM campaign";
$result = $connect->query($query);
$data = array();

while ($row = mysqli_fetch_array($result,MYSQL_ASSOC)) {
    $data[] = $row;
}
print json_encode($data);
mysql_close($connect);

?>

1 个答案:

答案 0 :(得分:1)

大多数情况下,当您获得服务器路由时,您会收到一个完整的响应对象而不仅仅是请求的数据,因此更改代码部分使得此调用看起来像这样:

{
    $http.get("php/overview.php")
     .success(function(response){
         console.log(response);
         $scope.campaigns = response.data;
         alert("It WORKED (DELETE Later)");
      })
      .error(function() {
          $scope.campaigns = "Could not fetch data";
          alert("DID NOT WORK (DELETE Later");
      });
}

应该做的伎俩。我还添加了一个console.log调用,以便您检查响应。

注意:不推荐使用成功和错误。建议您使用.then()来响应$ http调用。重构非常简短。