简体   繁体   中英

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

My friend already posted from another account and no one replied . I hope atleast somebody will help me this time. We are stuck very badly since many days. Did lot of troubleshooting but in vain !
we are trying to update data on the basis of id but it gives below error:

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

The link is working perfectly but unable to update data in the table. Below is my code for updating data in 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);
        });
    }  

and below is my web-api code for update:

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

Please dont mark it as a duplicate or inappropriate instead help us.
Thanks in advance :)

It doesn't appear that you have added you id to the object you are sending.

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

Which means that

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

Will return bad request.

Try add

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

You could also try to remove the json stringify as it should not be needed.

data: JSON.stringify(obj),

to

data : obj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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