簡體   English   中英

使用angular js和REST Web服務的PUT錯誤400錯誤請求

[英]PUT Error 400 bad request using angular js and REST web services

我的朋友已經從另一個帳戶發布了,沒有人回答。 我希望這次至少有人能幫助我。 自從很多天以來,我們的情況非常糟糕。 做了很多故障排除但是徒勞!
我們正在嘗試根據ID更新數據,但它給出以下錯誤:

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

鏈接運行正常,但無法更新表中的數據。 下面是我的代碼,用於更新angular 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);
        }

請不要將其標記為重復項或不當內容,請幫助我們。
提前致謝 :)

似乎您沒有將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字符串化,因為它不需要。

data: JSON.stringify(obj),

data : obj

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM