无法使用$ resource从AngularJS调用REST

时间:2014-01-22 20:57:34

标签: angularjs rest

我最近开始学习AngularJS并尝试使用$ resource从AngularJS调用REST服务。我的REST服务启动并运行,当我在浏览器中点击REST URL但是当我尝试调用时它给了我一个json输出它来自AngularJS没有任何反应。我确定我错过了一些东西。我已经搜索了这个问题,并且在stackoverflow上也看了类似的问题,但我无法解决它,因此我在这里问它。

service.js

var services = angular.module('myApp.services', ['ngResource']);

   services.factory('AngularIssues',
        function($resource){
        return $resource('http://localhost:8181/RestCXF5/services/UserInfo/:id', {} ,{
            get:{method:'GET' , params: {id: '@id'} }
         } );
    });

    services.value('version', '0.1');

controller.js

myApp.controller('fetchUserDetailsController', ['$scope', 'AngularIssues', function($scope, AngularIssues) {

    AngularIssues.get({id:1} , function(UserDetails) {
        $scope.UserDetails = UserDetails;
    });
}]);

在我的 index.html 中,我添加了

<script src="lib/angular/angular-resource.js"></script>

这就是我调用控制器的方式

<div class="col-lg-10" ng-controller="fetchUserDetailsController">
            <table ng-table="tableParams" class="table">
                <tr>
                    <td data-title=" 'Name' ">
                        {{UserDetails.firstName}}
                    </td>
                    <td>{{UserDetails.designation}}
                    </td>
                    <td>{{UserDetails.employeeId}}</td>
                </tr>
            </table>
        </div>

我的问题是我的REST服务根本没有被调用,而且我的html中有空白表。任何人都可以指出我在这里缺少的东西吗?谢谢!!

我甚至尝试使用 $ http服务,我的状态为400.这是代码。

myApp.controller('fetchUserDetailsController' ,
    ['$scope' , '$http' , function($scope , $http){
        $http.get('http://localhost:8181/RestCXF5/services/UserInfo/1').
            success(function(data) {

                $scope.UserDetails = data;

            }).
            error(function (data, status, headers, config) {
                alert("error" + status);
            });
    }]);

1 个答案:

答案 0 :(得分:2)

我认为这是错误的;

'http://localhost\\:8181/RestCXF5/services/UserInfo/:id'

您不能使用反斜杠\而是尝试

http://localhost:8181/RestCXF5/services/UserInfo/:id