AngularJs $resource 未调用自定义 'GET'
AngularJs $resource not invoking custom 'GET'
我在托管 here 的 .Net 中创建了一个简单的 RESTful 服务。
我正在尝试从我的 angularjs 代码中调用 getByName
操作。但是,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: {}"
当我签入时,fiddler 调用将转到 'http://publicapitest.azurewebsites.net/api/Employee'
而不是 'http://publicapitest.azurewebsites.net/api/Employee/getByName'
我错过了什么吗?
您应该指定方法 url 如下:
getByName : {method: 'GET', url: baseApiUrl + 'api/Employee/getByName', params: {} , isArray: false}
我在托管 here 的 .Net 中创建了一个简单的 RESTful 服务。
我正在尝试从我的 angularjs 代码中调用 getByName
操作。但是,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: {}"
当我签入时,fiddler 调用将转到 'http://publicapitest.azurewebsites.net/api/Employee'
而不是 'http://publicapitest.azurewebsites.net/api/Employee/getByName'
我错过了什么吗?
您应该指定方法 url 如下:
getByName : {method: 'GET', url: baseApiUrl + 'api/Employee/getByName', params: {} , isArray: false}