1

I'm trying to delete data by it's id, but it's not working. When I set the id of the data i want to delete, it works. I need your help.

My HTML code:

<div data-ng-controller="deletePinCtrl">
<button data-ng-click="deletePin()">delete</button>
    </div>
     <h3>{{pin.title}}</h3>
     <h4>{{pin.content}}</h4>

My Angular code:

.controller("deletePinCtrl", function($scope, $http){
$scope.deletePin = function() {
    var obj = { "id": "value"};
    var config = { data: JSON.stringify(obj) };
    $http.delete('http://localhost:8080/SpringMVCHibernate/rest/delete', config).
          then(function(response) {
            alert("pin is deleted");
          }, function(response) {
            alert("try again");
        });          
    }

})

  • Not related to the question itself: it doesn't seem to be a good idea to send the id in the body of the DELETE request. The id should rather be part of the URL, e.g. `DELETE http://localhost/pin/123` or `DELETE http://localhost/pin?id=123`. Doing `DELETE http://foo/delete {id in body}` is not very "RESTful", feels more like RPC-style. See also: [Is an entity body allowed for an HTTP DELETE request?](http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request) – pdenes Aug 18 '15 at 10:53

1 Answers1

0

pass you id in function

<button data-ng-click="deletePin(pin.id)">delete</button>

and set here

$scope.deletePin = function(value) {
    var obj = { "id": value}; // now id have its id 
Shailendra Sharma
  • 6,976
  • 2
  • 28
  • 48
  • I'm still getting these two errors: OPTIONS http://localhost:8080/SpringMVCHibernate/rest/delete 404 (Not Found), XMLHttpRequest cannot load http://localhost:8080/SpringMVCHibernate/rest/delete. Invalid HTTP status code 404 – Ardit Avdylaj Aug 18 '15 at 10:58