【问题标题】:$http.get(url) not working$http.get(url) 不工作
【发布时间】:2016-08-21 08:00:06
【问题描述】:

我正在使用 $http 使用 Angular js 从另一个文件中获取详细信息。

<body>
  <h2>AngularJS</h2>
  <div ng-app = "" ng-controller = "sController">

     <table>
        <tr>
           <th>Name</th>
           <th>Roll No</th>
           <th>Percentage</th>
        </tr>

        <tr ng-repeat = "student in students">
           <td>{{ student.Name }}</td>
           <td>{{ student.RollNo }}</td>
           <td>{{ student.Percentage }}</td>
        </tr>
     </table>
  </div>

<script>
     function studentController($scope,$http) {
        var url = "C:\\Users\\Test\\Desktop\\data.txt";

        $http.get(url).success( function(response) {
        alert('success');
           $scope.students = response;
        }).error(function (response) {
    alert("error");

    return status;
    });
    }
</script>      

data.txt 文件包含 json 格式的学生详细信息。

[{"Name" : "Smith","RollNo" : 11,"Percentage" : "80%"}]

控件没有进入成功。我错过了什么吗?我也没有得到任何内部回应。请帮忙。

【问题讨论】:

  • 你注册你的控制器了吗? (angular.module(moduleName).controller...
  • 为 Angular 创建一个模块并引用应用名称,同时使用应用名称注册控制器

标签: angularjs json http


【解决方案1】:

这是一个 http 请求,文件应该托管在某个地方,并且总是将 json 作为 json 扩展文件更好。

您的代码几乎没有问题,

(i) 我没有看到模块名称

(ii)您已将控制器定义为sController 并将其用作studentController

检查此示例

app.controller("MyController", ["$scope","$http",
    function($scope, $http) {
             $http.get('test.json').then(function (response){
                $scope.students = response.data;
                console.log(response.data);
        });

}]);

这是工作的sample

【讨论】:

    【解决方案2】:

    我已经修改了你的代码,我已经直接从文本中读取内容以便于理解,你可以添加“http”来从 URL 上下文中读取内容。

    此 AngularJS 代码已将正确的模块和控制器注册到模块部分,我已经对其进行了测试并附上了下面的屏幕截图。

    如果您有任何疑问,请告诉我。

     <!DOCTYPE html>
          <html ng-app="MyApp">
             <head>
              <meta charset="utf-8" />
              <title>AngularJS Plunker</title>
              <script data-require="angular.js@1.0.x" src="https://code.angularjs.org/1.0.8/angular.js" 
                 data-semver="1.0.8"></script>
              <script>
                 var app = angular.module('MyApp', []);
                 
                 
                 
                 app.controller('MainCtrl', function($scope) {
                   $scope.name = "World";
                 
                   var text = 
                 
                 '[{"Name" : "Smith","RollNo" : 111,"Percentage" : "80%"}]';
                 
                   var obj = JSON.parse
                 
                 (text);
                 
                   $scope.students = obj;
                 
                 });
              </script>
           </head>
               <body ng-
                  controller="MainCtrl">
                      <p>Hello {{name}}!</p>
                  <div ng-controller="MainCtrl">
                 <table>
                    <tr>
                       <th>Name</th>
                       <th>Roll No</th>
                       <th>Percentage</th>
                    </tr>
                    <tr ng-repeat="student in students">
                       <td>{{ student.Name }}</td>
                       <td>{{ student.RollNo }}</td>
                       <td>{{ student.Percentage }}</td>
                    </tr>
                 </table>
              </div>
           </body>
        </html>
    

    代码输出:

    【讨论】:

      【解决方案3】:

      请更正函数中的控制器名称。使用 sController 而不是 studentController 并且当您发出 $http 请求时,它将找到托管在某个 URL 中的文件 http://localhost 或任何其他 @987654325 @。

      HTML:

      <div ng-app = "MyApp" ng-controller = "sController">
      

      脚本:

      <script>
      var app = angular.module("MyApp",[]);
          app.controller("sController",function ($scope,$http) {
              var url = "http://www.example.com/data.txt";
      
              $http.get(url).success( function(response) {
              alert('success');
                 $scope.students = response;
              }).error(function (response) {
          alert("error");
      
          return status;
          });
          });
      </script> 
      

      【讨论】:

        猜你喜欢
        • 2018-03-16
        • 2014-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        • 2017-08-29
        相关资源
        最近更新 更多