遇到400错误请求(Angular+WebAPI)的问题

Having trouble with a 400 Bad Request (Angular + WebAPI)

本文关键字:问题 Angular+WebAPI 错误 请求 遇到      更新时间:2023-09-26

我是通过尝试构建一个跟踪漫画的数据库来学习的。我可以发布新的漫画,并且毫不费力地获得它们。但当我想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
默认情况下,IIS Express和IIS8中不启用PUT和DELETE。您可以通过以下步骤启用这些动词:
  1. 在计算机上打开applicationHost.config文件运行Web Api应用程序。该文件位于%userprofile%''documents''IIS{Express|8}''config"。

  2. 向下滚动到IIS的底部applicationHost.config文件并查找处理程序条目以开头

    <add name="ExtensionlessUrl-Integrated-4.0"...`. 
    
  3. 在"verb"属性中添加PUT和DELETE,因此"verb"属性看起来像:verb="GET,HEAD,POST,DEBUG,PUT,DELETE"