在AngularJS + WebAPI中使用$ http

经验

我正在尝试使用AngularJS在WebAPI上打印出类别列表。我有以下页面,当我导航至该页面时,会收到包含“ -1”的警报消息。

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="Scripts/angular.min.js"></script>
<script language="javascript">
    var myApp = angular.module('myApp', []);

    myApp.service('categoriesService', function ($http) {
        delete $http.defaults.headers.common['X-Requested-With'];
        this.getData = function () {
            // $http() returns a $promise that we can add handlers with
.then()
            return $http({
                method: 'GET',
                url: 'https://www.example.com/api/categories'
            });
        }
    });

    myApp.controller('CategoriesCtrl', function ($scope, categoriesService) {
        $scope.data = null;
        categoriesService.getData().then(function (response) {
            $scope.data = response;
        }, function (response) {
            alert(response.status);
        });
    });
</script>
</head>
<body ng-app="myApp">
    <div  ng-controller="CategoriesCtrl">
        <ul>
            <li ng-repeat="category in data">
                {{ category.Name }}
            </li>
        </ul>
    </div>
</body>
</html>

我究竟做错了什么?我从这里尝试了一些示例:如何在AngularJS中正确使用HTTP.GET?具体来说,是否需要外部API调用?这里AngularJS不显示API数据

c

您的服务返回了一个承诺,但是当该承诺得到解决时,将不会返回任何数据:

this.getData = function () {
  return $http.get('https://www.example.com/api/categories').then(
    function(response) {
      console.log(response);
      return response;
    },
    function(error) {
      console.log(error);
      return error;
    }
  });
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在AngularJS服务中使用$ http

来自分类Dev

AngularJS $ http和WebApi 2

来自分类Dev

在AngularJS服务中使用$ http回调

来自分类Dev

在http发布AngularJS异步中使用循环

来自分类Dev

Angularjs承诺使用$ http

来自分类Dev

在$ http回调中使用AngularJS和Typescript使用'this'

来自分类Dev

在angularjs-rails-resource中使用http缓存

来自分类Dev

Angularjs; 在服务中使用$ http返回引用而不是实际数据

来自分类Dev

AngularJS:如何在过滤器中使用$ http

来自分类Dev

在AngularJS中使用$ http服务时的可变范围

来自分类Dev

如何在AngularJS中使用重试逻辑重用HTTP请求

来自分类Dev

在AngularJS中使用$ resource和$ http管理全局错误

来自分类Dev

在angularjs中使用$ http向视图显示json文件

来自分类Dev

Angularjs; 在服务中使用$ http返回引用而不是实际数据

来自分类Dev

在angularjs中使用$ http向视图显示json文件

来自分类Dev

在angularjs-rails-resource中使用http缓存

来自分类Dev

在AngularJS中使用$ http服务发布多个数组

来自分类Dev

如何在AngularJS中使用Jasmine测试$ http

来自分类Dev

在 AngularJS 的 .service 中使用 $http 未定义“get”

来自分类Dev

使用$ http时加载AngularJS

来自分类Dev

基于WebAPI + AngularJS中的DateTime的动态HTTP请求

来自分类Dev

在angularjs中调用http(RESTFUL WebAPI)的正确方法

来自分类Dev

AngularJS:使用超时属性取消$ http请求

来自分类Dev

使用Http JSON AngularJS 2时出错

来自分类Dev

如何使用AngularJS启动$ http $ cache

来自分类Dev

使用AngularJS发送HTTP标头请求

来自分类Dev

使用$ http在AngularJS中调用cURL

来自分类Dev

AngularJS $ http请求在Controller内部使用forEach

来自分类Dev

使用$ http服务的AngularJS跨域请求

Related 相关文章

热门标签

归档