参考错误:未定义响应AngularJs

时间:2018-05-01 07:22:27

标签: javascript html

我正在关注此tutorial以从数据库中检索数据。来自服务器的已检索数据是一个json数组,它包含以下两个对象:

[{"_id":{"$oid":"5ae81566f3a9bd1fe8002d34"},"Customer_Name":"cXQzbXlUeHRVckFvVGpoVUQxaHNoQT09","Responsiple_User":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Phone_Number":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","ID_Number":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Sale":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Resposible_Sales":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","City":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Job":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Monhthly_Income":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Number_Of_Months":"SVFxZVJkWG0vaUhxSUxFNzdrWjZCZz09","Monthly_installment_of_the_client":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","First_Payment":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Bank":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Financial_Situation":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Account":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Notes":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Date":"2018-05-01 09:21:10"},{"_id":{"$oid":"5ae8157ff3a9bd1fe8002d36"},"Customer_Name":"aGxveGF1SGxKWXpFajgzSGI0RmE0QT09","Responsiple_User":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Phone_Number":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","ID_Number":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Sale":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Resposible_Sales":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","City":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Job":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Monhthly_Income":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Number_Of_Months":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Monthly_installment_of_the_client":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","First_Payment":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Bank":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Financial_Situation":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Account":"SVFxZVJkWG0vaUhxSUxFNzdrWjZCZz09","Notes":"Wjc0TFpMbUtyUlo1bGptZ1RCOG84Zz09","Date":"2018-05-01 09:21:35"}]

我修改了教程中的代码,只显示表中的json数据。问题是我一直没有定义响应问题。以下是我的代码

<!DOCTYPE html>  
 <!-- index.php !-->  
 <html>  
      <head>  
           <title>test</title>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
      </head>  
      <body>  
           <br /><br />  
           <div class="container" style="width:500px;">  
                <h3 align="center">AngularJS Tutorial with PHP - Fetch / Select Data from Mysql Database</h3>  
                <div ng-app="myapp" ng-controller="usercontroller" ng-init="displayData()">  

                     <table class="table table-bordered">  
                          <tr>  
                               <th>First Name</th>  
                               <th>Last Name</th>  
                          </tr>  
                          <tr ng-repeat="x in names">  
                               <td>{{x.City}}</td>  
                               <td>{{x.last_name}}</td>  
                          </tr>  
                     </table>  
                </div>  
           </div>  
      </body>  
 </html>  
 <script>  
 var app = angular.module("myapp",[]);  
 app.controller("usercontroller", function($scope, $http){  

      $scope.displayData = function(){  
      alert ("s");
           $http.get("data.php",$scope.names = response ).success(function(response){});  

      }  
 });  
 </script>  

1 个答案:

答案 0 :(得分:0)

更改您的函数正文并获取此

的请求
$http.get("data.php").success(function(response){
    $scope.names = response;
});