简体   繁体   English

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

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

I keep getting a 400 Bad Request when trying to update the reputation-field of my application. 尝试更新应用程序的信誉字段时,我总是收到400错误的请求。 What it's supposed to do is to save the value once the reputation has been increased or decreased. 它的目的是在声誉提高或降低后保存价值。 It recognizes the id of the post so that shouldn't be a problem. 它可以识别帖子的ID,因此应该不会有问题。 Note that I'm very new to AngularJS so I this may be very easy, but I have no idea. 请注意,我对AngularJS还是很陌生,所以这可能很容易,但是我不知道。

Here's the backend: 这是后端:

// 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);
        }

The function: 功能:

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

The factory: 该工厂:

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

And the items from the view: 以及视图中的项目:

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

I believe you want to be passing the resource object itself as the second argument to Post.update : 我相信您希望将资源对象本身作为Post.update的第二个参数Post.update

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

IIRC you should also be able to just do: IIRC您还应该能够做到:

post.update();

Take a look at the $resource docs and scroll down to the bit about creating a custom put request. 看一下$ resource文档,然后向下滚动至有关创建自定义放置请求的内容。

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

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