代码之家  ›  专栏  ›  技术社区  ›  user2243747

AngularJs$resource未调用自定义“GET”

  •  0
  • user2243747  · 技术社区  · 8 年前

    我在中创建了一个简单的RESTful服务。Net托管 here . 我试图调用 getByName 来自angularjs代码的操作。但是,angularjs代码正在调用默认值' get '代替' getByName '

    AngularJs代码:

    app.factory('UserResourceSvc',function($resource){
        var baseApiUrl = "http://publicapitest.azurewebsites.net/";
        //var baseApiUrl = "http://localhost:92/"
        return $resource( baseApiUrl + 'api/Employee/:id',{id: "@id"},
            { 
                getByName : {method: 'GET', params: {} , isArray: false} 
            }
        );    
    });
    

    我在点击按钮时调用下面的函数来触发API调用。

     $scope.getByName = function(){
       UserResourceSvc.getByName(function(data){
          debugger;
       })
     }
    

    我收到以下错误消息:

    angular.js:14794 Error: [$resource:badcfg] Error in resource configuration for action `getByName`. Expected response to contain an object but got an array (Request: GET http://publicapitest.azurewebsites.net/api/Employee)
    http://errors.angularjs.org/1.6.7/$resource/badcfg?p0=getByName&p1=object&p2=array&p3=GET&p4=http%3A%2F%2Fpublicapitest.azurewebsites.net%2Fapi%2FEmployee
        at angular.js:116
        at $http.then.response.resource (angular-resource.js:757)
        at processQueue (angular.js:17145)
        at angular.js:17193
        at Scope.$digest (angular.js:18331)
        at Scope.$apply (angular.js:18628)
        at done (angular.js:12619)
        at completeRequest (angular.js:12863)
        at XMLHttpRequest.requestLoaded (angular.js:12780) "Possibly unhandled rejection: {}"
    

    'http://publicapitest.azurewebsites.net/api/Employee' 而不是 'http://publicapitest.azurewebsites.net/api/Employee/getByName'

    我错过什么了吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   lzagkaretos    8 年前

    您应该按如下方式指定方法url:

    getByName : {method: 'GET', url: baseApiUrl + 'api/Employee/getByName', params: {} , isArray: false}