Angular使用Ajax获取JSON数据 [英] Angular get JSON data with ajax

查看:117
本文介绍了Angular使用Ajax获取JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过带有角度的ajax获取json数据?我尝试了很多,但是我的代码无法正常工作. 我的代码:

How can I get json data via ajax with angular? I tried a lot but my code is not working. My code:

<!DOCTYPE html>
<html lang="en" ng-app="test">

<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="https://code.angularjs.org/1.6.1/angular.min.js"></script>
  <script>
    angular.module('test', [])
      .controller('TestCtrl', function($scope, $http) {
        $scope.data = [];
        $http.get("http://jsonplaceholder.typicode.com/users").then(function(res) {
          $scope.data = JSON.parse(res);
        });
      });
  </script>
</head>

<body ng-controller="TestCtrl">
  <div ng-repeat="item in data">
    {{item.id}}
  </div>
</body>

</html>

推荐答案

您是否尝试过这样..

<script>
        var app = angular.module('test', [])
        app.controller('TestCtrl', function ($scope, $http) {
            $http.get("http://jsonplaceholder.typicode.com/users").then(function(res,status,xhr) {
                $scope.data = res.data;
            });
        });
    </script>

这篇关于Angular使用Ajax获取JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆