使用 angularJS 中的 $resource 将 id 发送到 DELETE API
Sending id to DELETE API using $resource in angularJS
我的 angularJS 服务有这个带有 $resource 的删除方法,
return $resource(apiUrl, {}, {
delete: {
url: apiUrl+ ":itemId",
params: { itemId: "@itemId" },
method: 'DELETE'
}
});
但是在尝试调用 delete 方法时,从服务发送的请求没有 itemId。
dataService.delete({ itemId: itemId}, {})
.$promise
.then((data) => {
// Statements
})
.catch((error) => {
// Logging
})
.finally(() => {});
URL 应该调用的是 https://url:someport/v1.0/items/{itemId}
然而目前的 url 是 https://url:someport/v1.0/items
有没有办法使用当前的 $resource 代码得到这个母鹿?
我认为您需要使用第二个参数来指定请求的参数。
return $resource(apiUrl, {itemId: '@itemId'}, {
delete: {
method: 'DELETE'
}
});
您可以在此处查看完整文档 https://docs.angularjs.org/api/ngResource/service/$resource。这会给你更好的主意。您需要在第二个参数(对象)中提供参数。
var User = $resource('/user/:userId',
{userId: '@id'},
{delete: {
method: 'DELETE' }
});
User.delete({userId: 123}).$promise.then(function(user) {
// perform other operations
});
这 code 发送正确的 url:
angular.module("app",['ngResource'])
.controller("ctrl",function($scope,$resource){
var url = "//requestbin.fullcontact.com/1ooe8eb1";
var dataService = $resource(url, {itemId: "@itemId"}, {
delete: {
url: url+"/items/:itemId",
params: { itemId: "@itemId" },
method: 'DELETE'
}
});
$scope.delete = function() {
var res = dataService.delete({itemId:77});
res.$promise.finally(function(){
$scope.result="DONE";
});
};
});
<script src="//unpkg.com/angular/angular.js"></script>
<script src="//unpkg.com/angular-resource/angular-resource.js"></script>
<body ng-app="app" ng-controller="ctrl">
<button ng-click="delete()">Delete Item</button>
{{result}}
</body>
由于 CORS,响应被浏览器阻止,但检查在:
https://requestbin.fullcontact.com/1ooe8eb1?inspect
表明 URL 的格式正确。
我的 angularJS 服务有这个带有 $resource 的删除方法,
return $resource(apiUrl, {}, {
delete: {
url: apiUrl+ ":itemId",
params: { itemId: "@itemId" },
method: 'DELETE'
}
});
但是在尝试调用 delete 方法时,从服务发送的请求没有 itemId。
dataService.delete({ itemId: itemId}, {})
.$promise
.then((data) => {
// Statements
})
.catch((error) => {
// Logging
})
.finally(() => {});
URL 应该调用的是 https://url:someport/v1.0/items/{itemId} 然而目前的 url 是 https://url:someport/v1.0/items
有没有办法使用当前的 $resource 代码得到这个母鹿?
我认为您需要使用第二个参数来指定请求的参数。
return $resource(apiUrl, {itemId: '@itemId'}, {
delete: {
method: 'DELETE'
}
});
您可以在此处查看完整文档 https://docs.angularjs.org/api/ngResource/service/$resource。这会给你更好的主意。您需要在第二个参数(对象)中提供参数。
var User = $resource('/user/:userId',
{userId: '@id'},
{delete: {
method: 'DELETE' }
});
User.delete({userId: 123}).$promise.then(function(user) {
// perform other operations
});
这 code 发送正确的 url:
angular.module("app",['ngResource'])
.controller("ctrl",function($scope,$resource){
var url = "//requestbin.fullcontact.com/1ooe8eb1";
var dataService = $resource(url, {itemId: "@itemId"}, {
delete: {
url: url+"/items/:itemId",
params: { itemId: "@itemId" },
method: 'DELETE'
}
});
$scope.delete = function() {
var res = dataService.delete({itemId:77});
res.$promise.finally(function(){
$scope.result="DONE";
});
};
});
<script src="//unpkg.com/angular/angular.js"></script>
<script src="//unpkg.com/angular-resource/angular-resource.js"></script>
<body ng-app="app" ng-controller="ctrl">
<button ng-click="delete()">Delete Item</button>
{{result}}
</body>
由于 CORS,响应被浏览器阻止,但检查在:
https://requestbin.fullcontact.com/1ooe8eb1?inspect
表明 URL 的格式正确。