2014-11-02 75 views
2

我嘗試刪除時得到以下響應:405方法不允許。 在我的日誌中有寫入允許GET,但DELETE不允許。錯誤405方法不允許錯誤,發送DELETE到服務器時

的Java:

@ResponseBody 
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}") 
    public void delete(@PathVariable String id) { 
    speakerService.delete(id); 
    } 

Angularjs

app.factory('SpeakerResource', function ($resource) { 
     return $resource('rest/speaker/:speakerId', 
    { 
     speakerId: '@speakerId' 
    }, 
    { 
     'update': { method: 'PUT' } 
    }, 
    { 
     'delete': { method: 'DELETE', params: { 'id': 'speakerId' }} 

    } 
    ) 
}); 

SpeakerService

this.delete = function (id, callback) { 

    SpeakerResource.delete({ speakerId: id }, function() { 
     callback(); 
    }); 

} 

回答

4

我不知道你完整的代碼,而我不是AngularJS的專家,但它看起來像你想發送一個DELETE請求到URL <hopefullySomething>/{id}Path var有效的)。但它看起來像你發送DELETE請求,所以一些URL的參數ID爲<hopefullySomething>?id={id}請求參數)。

這個問題和答案解釋的路徑變量,請求之間的差參數多一點@RequestParam vs @PathVariable

3

使用$ http.delete(),並返回例如狀態數據,我只是測試用的彈簧下面,且運行正常

@RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE) 
    public @ResponseBody Status deletePerson(@PathVariable("id") int id) {  
      try { 
       personService.removePerson(id); 
      return new Status(1, "person deleted Successfully !"); 
      } catch (Exception e) { 
      return new Status(0, e.toString()); 
      }  
    } 

angular.module('personService', []) 

.factory('Person', ['$http',function($http) { 
    return { 

     deletePerson: function(id) { 
      return $http.delete('/restperson/delete/'+id); 
     } 
    } 
}]); 

控制器

angular.module('personController', []) 

// inject the person service factory into our controller 
.controller('mainController', ['$scope','$http','Person', function($scope, $http, Person) {  

    //delete 
    $scope.deletePerson = function(id) { 
     Person.deletePerson(id) 
      .success(function(data) { 
       $scope.message = data; 
      }); 
    }; 

}]); 
相關問題