[英]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.