简体   繁体   English

遇到400错误请求(Angular + WebAPI)的麻烦

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

I am learning by trying to build a database that tracks comics. 我正在通过尝试建立一个跟踪漫画的数据库来学习。 I can POST new comics and GET them with no trouble. 我可以发布新漫画并轻松获得它们。 But when I'd like to PUT, I run into a problem. 但是,当我想进行PUT时,我遇到了一个问题。 I keep getting a bad request sent, but I think all the information is correct. 我一直收到错误的请求,但我认为所有信息都是正确的。 I think all my info matches up, but I'm not sure what else is wrong. 我认为我所有的信息都匹配,但是我不确定还有什么问题。

All I am trying to do is update the comic list so you can track your physical and digital copies of the comic. 我要做的就是更新漫画列表,以便您可以追踪漫画的物理和数字副本。

Thanks in advance. 提前致谢。

Here is my DBController.cs: 这是我的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");
    }
}

Here is my CollectionController.js: 这是我的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) {
        })
}

And my ApiFactory.js 还有我的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,
    }

Here is my .html: 这是我的.html:

<button class="btn btn-danger"  ng-click="updateComic(x.ComicId)">Save</button>

And lastly, my error messages. 最后,我的错误信息。 Sorry, not really sure how/what you need. 抱歉,不太确定您的需求。 Last night when I was figuring this out, I had clicked on the network tab and was ble to find inner exceptions and such. 昨天晚上,当我弄清楚这一点时,我单击了“网络”选项卡,可以找到内部异常等。 Either I can't find them this time, or I didn't get any. 这次我找不到他们,或者我什么也没得到。 But this is from my JS console: 但这来自我的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 and DELETE are not enabled in IIS Express and IIS8 by default. 默认情况下,IIS Express和IIS8中未启用PUT和DELETE。 You can enable these verbs by following these steps: 您可以按照以下步骤启用这些动词:

  1. Open the applicationHost.config file on the machine running the Web Api application. 在运行Web Api应用程序的计算机上打开applicationHost.config文件。 The file is located at %userprofile%\\documents\\IIS{Express|8}\\config”. 该文件位于%userprofile%\\ documents \\ IIS {Express | 8} \\ config”。

  2. scroll down to the bottom of the IIS applicationHost.config file and look for a handler entry that starts with: 向下滚动到IIS applicationHost.config文件的底部,然后查找以以下内容开头的处理程序条目:

     <add name="ExtensionlessUrl-Integrated-4.0"...`. 
  3. In the "verb" attribute add PUT and DELETE so the "verb" attribute looks like: verb="GET,HEAD,POST,DEBUG,PUT,DELETE" 在“动词”属性中添加PUT和DELETE,以使“动词”属性看起来像:verb =“ GET,HEAD,POST,DEBUG,PUT,DELETE”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM