2015-08-18 24 views
2

我试图通过它的ID删除数据,但它不工作。 当我设置我想要删除的数据的ID时,它的工作原理。我需要你的帮助。如何使用AngularJs通过它的id删除数据?

我的HTML代码:

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

我的角度代码:

.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"); 
     });   
    } 

})

+0

与问题本身无关:在DELETE请求的正文中发送id似乎不是一个好主意。 id应该是URL的一部分,例如'删除http:// localhost/pin/123'或'删除http:// localhost/pin?id = 123'。做'删除http:// foo/delete {id in body}'不是很“RESTful”,感觉更像RPC风格。另请参见:[是否允许实体主体使用HTTP DELETE请求?](http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request) – pdenes

回答

0

通您在功能

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

ID和这里设置

$scope.deletePin = function(value) { 
    var obj = { "id": value}; // now id have its id 
+0

我仍然遇到这两个错误: 选项http:// localhost:8080/SpringMVCHibernate/rest/delete 404(Not Found), XMLHttpRequest无法加载http:// localhost:8080/SpringMVCHibernate/rest/delete。无效的HTTP状态码404 –