繁体   English   中英

400(错误请求)-在AngularJS中更新值时

[英]400 (Bad Request) - When updating value in AngularJS

尝试更新应用程序的信誉字段时,我总是收到400错误的请求。 它的目的是在声誉提高或降低后保存价值。 它可以识别帖子的ID,因此应该不会有问题。 请注意,我对AngularJS还是很陌生,所以这可能很容易,但是我不知道。

这是后端:

// PUT api/Post/5
        public IHttpActionResult PutPost(int id, Post post)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != post.pid)
            {
                return BadRequest();
            }

            db.Entry(post).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

功能:

$scope.increment = function (post) {
    post.reputation += 1;
    Post.update({ id: post.pid });
}

该工厂:

droppeApp.factory('Post', function ($resource) {
    return $resource('/api/Post/:id', { id: '@id' }, {
        update: { method: 'PUT' },
        delete: { method: 'DELETE' }
    });
});

以及视图中的项目:

<span class="decrease right" ng-click="decrease(post);">-</span>
<span class="increment right" ng-click="increment(post);">+</span>

我相信您希望将资源对象本身作为Post.update的第二个参数Post.update

Post.update({id: post.id}, post);

IIRC您还应该能够做到:

post.update();

看一下$ resource文档,然后向下滚动至有关创建自定义放置请求的内容。

暂无
暂无

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

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