我得到的错误,这意味着它不会从angular执行我的删除方法:
DELETE http://localhost:49717/api/Item?iid=D1 405 (Method Not Allowed)
我的app.js
'use strict';
var SalesApp = angular.module('SalesApp', ['ngRoute', 'ngResource']).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/', { controller: ItemCtrl, templateUrl: 'item.html' }).
when('/new', { controller: CreateCtrl, templateUrl: 'details.html' }).
when('/edit/:editId', { controller: EditCtrl, templateUrl: 'details.html' }).
otherwise({ redirectTo: '/' });
}]);
SalesApp.factory('Item', function ($resource) {
return $resource('/api/Item/:id', { id: '@id' }, { update: { method: 'PUT' } });
});
var ItemCtrl = function ($scope, $location, Item) {
$scope.delete = function () {
var id = this.item.iid;
Item.delete({ iid: id }), function () {
$('#item_' + id).fadeOut();
};
};
};
index.html的html的适当部分
<tbody>
<tr ng-repeat="item in items" id="item_{{item.iid}}">
<td>{{item.iid}}</td>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td><a ng-click="delete()">X</a></td>
<td><a href="#/edit/{{item.iid}}"><div class="edit">Edit</div></a></td>
</tr>
</tbody>
我的脚本导入:
<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/angular.js"></script>
<script src="Scripts/angular-resource.js"></script>
<script src="Scripts/app.js"></script>
<script src="Scripts/angular-resource.js"></script>
<script src="Scripts/angular-sanitize.js"></script>
<script src="Scripts/angular-animate.js"></script>
<script src="Scripts/angular-touch.js"></script>
<script src="Scripts/angular-route.js"></script>
这可能是我的一些愚蠢的错误,我不太确定,添加和更新是否有效?我的第一个想法是,它可能是一些外键令人讨厌,但我尝试使用sql server删除一个项目,它工作得很好。
添加了后端:
// DELETE api/Item/5
[ResponseType(typeof(Item))]
public IHttpActionResult DeleteItem(string id)
{
Item item = db.Item.Find(id);
if (item == null)
{
return NotFound();
}
db.Item.Remove(item);
db.SaveChanges();
return Ok(item);
}
答案 0 :(得分:0)
您需要在$http({url:"your-url-here",method:"DELETE",params:{your-parameters}})
之前添加以下代码
确保DELETE正常工作的行。
$http.defaults.headers["delete"] = {
'Content-Type':'application/json,charset=utf-8'
};