$ http.get(url)无效

时间:2016-08-21 08:00:07

标签: angularjs json http

我正在使用$ j来使用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%"}]

控件不会成功。我错过了什么吗?此外,我没有得到任何内部回应。请帮忙。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我修改了您的代码,我已经在其中直接从文本中读取内容以使其易于理解,您可以添加“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>

代码输出: enter image description here

答案 2 :(得分:-1)

请更正功能中的控制器名称。使用sController而不是studentController,并且当您发出$http请求时,它会找到托管在某个网址中的文件{{ 1}}或任何其他http://localhost

Html:

domain

脚本:

<div ng-app = "MyApp" ng-controller = "sController">
相关问题