【发布时间】:2015-06-02 13:45:27
【问题描述】:
我正在尝试建立一个跟踪漫画的数据库来学习。我可以发布新漫画并轻松获取它们。但是当我想 PUT 时,我遇到了问题。我不断收到错误的请求,但我认为所有信息都是正确的。我认为我的所有信息都匹配,但我不确定还有什么问题。
我要做的只是更新漫画列表,以便您跟踪漫画的实体和数字副本。
提前致谢。
这是我的 DBController.cs:
[Authorize]
public IHttpActionResult Put(Comic comic)
{
string UserId = User.Identity.GetUserId();
if (UserId == null) return BadRequest("You Must Be Logged In to Edit");
else if (comic.UserId == UserId || User.IsInRole("Admin"))
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
var currentComic = db.Comics.Find(comic.ComicId);
currentComic.IsDigital = comic.IsDigital;
currentComic.IsPhysical = comic.IsPhysical;
db.SaveChanges();
}
return Ok();
}
else return BadRequest("Insufficient privileges");
}
}
这是我的 CollectionController.js:
$scope.physical = false;
$scope.digital = false;
$scope.updateComic = function (ComicId) {
var comic = {
ComicId: ComicId,
IsPhysical: $scope.physical,
IsDigital: $scope.digital,
}
return MarvelApiFactory.editComic(comic).then(function (data) {
})
}
还有我的 ApiFactory.js
var editComic = function (comic) {
var deferred = $q.defer();
$http({
url: '/api/ComicDB',
method: "PUT",
headers: { Authorization: "Bearer " + localStorage.getItem('token') },
data: comic
}).success(function () {
deferred.resolve();
}).error(function () {
deferred.reject();
})
return deferred.promise;
}
return {
editComic: editComic,
}
这是我的 .html:
<button class="btn btn-danger" ng-click="updateComic(x.ComicId)">Save</button>
最后,我的错误信息。抱歉,不太确定您需要什么/如何。昨晚当我弄清楚这一点时,我点击了网络选项卡并能够找到内部异常等。要么这次我找不到它们,要么我没有得到任何东西。但这是来自我的 JS 控制台:
PUT http://localhost:53612/api/ComicDB 400 (Bad Request)angular.js:9827 (anonymous function)angular.js:9628 sendReqangular.js:9344 serverRequestangular.js:13189 processQueueangular.js:13205 (anonymous function)angular.js:14401 Scope.$evalangular.js:14217 Scope.$digestangular.js:14506 Scope.$applyangular.js:21440 (anonymous function)jquery-1.10.2.js:5109 jQuery.event.dispatchjquery-1.10.2.js:4780 elemData.handle
【问题讨论】:
-
服务器是否设置为接受
PUT请求?不知道为什么当错误消息显示 400 时你说它是 500 -
据我所知,确实如此。我将不得不做一些谷歌搜索,以便我可以仔细检查。抱歉,这是 400 个错误请求。发帖前我没有重读我的标题。
-
如果您通过 fiddler 代理这些调用,您将能够获得更多信息。 telerik.com/fiddler
-
您确定您的请求已满足 [Authorize] 限制条件吗?
-
我想是的。当我对其他动词进行故障排除时,我收到了另一条与权限有关的错误消息。
标签: javascript angularjs asp.net-web-api