PUT使用角度js和REST Web服务错误400错误请求

时间:2016-03-03 12:47:28

标签: javascript angularjs web-services rest ionic-framework

我的朋友已经从另一个帐户发布,没有人回复。我希望这次至少有人会帮助我。很多天以来我们都陷入了困境。做了很多故障排除但是徒劳无功! 我们正在尝试根据id更新数据,但它会给出以下错误:

PUT http://localhost:17681/api/perItemDetails/2 400 (Bad Request)  

链接工作正常,但无法更新表中的数据。 下面是我在角度js中更新数据的代码:

$scope.updateSave=function()
    {
        var updateid=this.meraId;
        var bb2price=this.bbprice;
        var maxbbPrice=this.mpbbprice;
        var vipPrice=this.vip_price;
        var retailPrice=this.retailprice;
        var sname=this.sname;
        var ptype=this.ptype;
        var useragent=this.userAgent;
        var obj = {
            bbPrice: bb2price,
            maxbbPrice: maxbbPrice,
            vipPrice: vipPrice,
            retailPrice:retailPrice,
            userNames:useragent,
            pType:ptype,
            sName:sname
        };

        $http({
            method: 'put',
            url: "http://localhost:17681/api/perItemDetails/" + updateid,
            data: JSON.stringify(obj),
            headers: {
                'Content-Type': 'application/json'
            }
        }).
        success(function (data, status, headers, config) {
            alert("updated succesfully");
        }).
        error(function (data, status, headers, config) {
            console.log('Error: ' + status);
        });
    }  

以下是我的更新的web-api代码:

// PUT: api/perItemDetails
        [ResponseType(typeof(void))]
        public IHttpActionResult PutperItemDetail(int id, perItemDetail perItemDetail)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

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

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

            return StatusCode(HttpStatusCode.NoContent);
        }

请不要将其标记为重复或不恰当而是帮助我们 在此先感谢:)

1 个答案:

答案 0 :(得分:0)

您似乎没有将ID添加到要发送的对象中。

   var obj = {
            bbPrice: bb2price,
            maxbbPrice: maxbbPrice,
            vipPrice: vipPrice,
            retailPrice:retailPrice,
            userNames:useragent,
            pType:ptype,
            sName:sname
        };

这意味着

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

将返回错误的请求。

尝试添加

   var obj = {
            id: updateId, 
            bbPrice: bb2price,
            maxbbPrice: maxbbPrice,
            vipPrice: vipPrice,
            retailPrice:retailPrice,
            userNames:useragent,
            pType:ptype,
            sName:sname
        };

您也可以尝试删除json stringify,因为它不需要。

data: JSON.stringify(obj),

data : obj