简体   繁体   English

使用AngularJS控制器的HTTP请求

[英]Http request using AngularJS controller

I am trying to show the data coming from a server, I tried to make a controller and copy the response to a property of scope. 我试图显示来自服务器的数据,我试图制造一个控制器并将响应复制到scope属性。 I tried 'response' and 'response.data' and none of them worked. 我尝试了“ response”和“ response.data”,但它们都没有起作用。

the html file looks like this: html文件如下所示:

<!DOCTYPE html>
<html>
<head>
    <script src="angular.min.js"></script>
    <title>Service Call ...</title>
</head>

<body>
    <script>
        var app = angular.module('myApp', []);

        app.controller('customersCtrl', function($scope, $http) {
            $http.get("https://linkToServer.json")
                .success(function(response) {$scope.trainings = response.data;
            });
        });
    </script>

    <div ng-app="myApp" ng-controller="customersCtrl">  
        <ul>
            <li ng-repeat="tr in trainings">
                {{ tr.id }}
            </li>
        </ul>
    </div>
</body>
</html>

The json structure is something like: json结构类似于:

[
  {
    "id" : "sm1001",
    "name" : "Lu",
  },
{
    "id" : "lm9898",
    "name" : "Di",
  },
  ....
]

What is wrong? 怎么了?

Plunkr of the problem 问题的Plunkr

Your plunk worked when I corrected the json. 当我更正json时,您的朋克工作了。 You did not include the json in the sample. 您没有在示例中包含json。 I copied it from your question. 我从您的问题中复制了它。

"name" : "Lu",

needs to be 需要是

"name" : "Lu"

http://plnkr.co/edit/toSicojh58y1lWrziyI8?p=preview http://plnkr.co/edit/toSicojh58y1lWrziyI8?p=preview

This might be only your sample data. 这可能只是您的示例数据。 The angular code is working fine. 角度代码工作正常。

I think it's because your html is all messed up. 我认为这是因为您的html全部搞砸了。 First a div , then a head tag I've updated your plunkr, now it's working: http://plnkr.co/edit/6Y1dX9vKkDANdZyEd0aj?p=preview 首先是div ,然后是head标签,我已经更新了您的plunkr,现在可以正常使用了: http ://plnkr.co/edit/6Y1dX9vKkDANdZyEd0aj?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM