[英]DELETE in angularjs - 405 Method not allowed
我得到的錯誤,這意味着它將無法從angular執行我的delete方法:
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);
}
您需要在$http({url:"your-url-here",method:"DELETE",params:{your-parameters}})
行之前放置以下代碼,以確保DELETE正常運行。
$http.defaults.headers["delete"] = { 'Content-Type':'application/json,charset=utf-8' };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.