如何使用$resource将HTTP DELETE请求发送到我的服务器?

时间:2023-01-24 19:33:37

I have the following code:

我有以下代码:

var entityResource = $resource('/api/' + $scope.entityType + '/');

entityResource.delete(entityId,
   function (result) {
      $scope.remove($scope.grid.data, idColumn, entityId);
   }, function (result) {
      alert("Error: " + result);
   })

When the code executes I get the following message:

当代码执行时,我得到以下消息:

DELETE http://127.0.0.1:81/api/Subject 404 (Not Found) 

{"message":"No HTTP resource was found that matches the request URI 'http://127.0.0.1:81/api/Subject'.","messageDetail

How can I make it so the $resource sends a DELETE with /api/Subject/123 Right now it seems to ignore the value of entityId

我怎样才能使$resource发送一个DELETE with /api/Subject/123现在它似乎忽略了entityId的值?

Here is what I tried so far after advice:

以下是我在此之前的建议:

    var entityResource = $resource('/api/:entityType/:entityId',
           { entityType: '@type', entityId: '@id' });
    entityResource.delete({ type: $scope.entityType, id: entityId }, 
        function (result) {
            $scope.remove($scope.grid.data, idColumn, entityId);
            $scope.remove($scope.grid.backup, idColumn, entityId);
            $scope.close();
        }, function (result) {
            alert("Error: " + result);
        })

Unfortunately this give me: DELETE /api?id=12&type=Subject HTTP/1.1

不幸的是,这给了我:删除/api?id = 12 +类型= HTTP / 1.1

1 个解决方案

#1


3  

You need to set up the entity ID in your template:

您需要在模板中设置实体ID:

var entityResource = $resource('/api/:entityType/:entityId', 
                               {type:'@entityType', id: '@entityId'});

Then when you call it, pass in the parameters:

然后当您调用它时,传入参数:

entityResource.delete({type: $scope.entityType, id: entityId}, success, failure);

or

var entity = new entityResource({type: $scope.entityType, id: entityId});
entity.$delete(success, failure);

assuming that success and failure look loke this:

假设成功和失败看起来是这样的:

var success = function (result) {
    $scope.remove($scope.grid.data, idColumn, entityId);
};

var failure = function (result) {
    alert("Error: " + result);
};

#1


3  

You need to set up the entity ID in your template:

您需要在模板中设置实体ID:

var entityResource = $resource('/api/:entityType/:entityId', 
                               {type:'@entityType', id: '@entityId'});

Then when you call it, pass in the parameters:

然后当您调用它时,传入参数:

entityResource.delete({type: $scope.entityType, id: entityId}, success, failure);

or

var entity = new entityResource({type: $scope.entityType, id: entityId});
entity.$delete(success, failure);

assuming that success and failure look loke this:

假设成功和失败看起来是这样的:

var success = function (result) {
    $scope.remove($scope.grid.data, idColumn, entityId);
};

var failure = function (result) {
    alert("Error: " + result);
};