【问题标题】:Having trouble with a 400 Bad Request (Angular + WebAPI)遇到 400 错误请求问题(Angular + WebAPI)
【发布时间】: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


【解决方案1】:

默认情况下,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"

【讨论】:

  • 谢谢你,我按照你说的更新了一切。但什么都没有改变。我会使用你之前推荐的代理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
  • 2020-07-10
  • 2017-07-20
  • 1970-01-01
  • 2018-04-10
  • 2017-07-23
  • 1970-01-01
相关资源
最近更新 更多