gpt4 book ai didi

jquery - 使用 ajax Post 编辑数据

转载 作者:行者123 更新时间:2023-12-01 05:52:13 24 4
gpt4 key购买 nike

我尝试更改表单中的数据然后保存。它没有更改而是添加了一条记录..

这是我的脚本:

$('#UpdateObject').click(function () {

var ObjnameEntered = $("#NameOB")[0].value;
var BasenameEntered = $("#selbaseID")[0].value;
var idsave = $("#labelobID")[0].value;
var kaartX = $("#mapx")[0].value;
var kaartY = $("#mapy")[0].value;
var picture = $("#trainpicname")[0].value;
$.ajax({
url: '/api/Traininglocation/' + idsave +'/',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ TrainLocationName: ObjnameEntered, TrainBaseID: BasenameEntered, TrainXcoord: kaartX, TrainYcoord: kaartY, TrainPicture: picture }),
dataType: 'json',
success: $('#doebam').show()
});
});

Controller :我使用了 put 方法,然后在发布我的项目后收到 405 错误..

error on the server hosing : Event code: 4005 Event message: Forms authentication failed for the request. Reason: The ticket supplied was invalid. Event time: 2-12-2013 16:21:56 Event time (UTC): 2-12-2013 15:21:56 Event ID: 817a9928807646699d3d2ef795ae478d Event sequence: 2 Event occurrence: 1 Event detail code: 50201

//PUT api/Traininglocation/5

    public HttpResponseMessage PutTrainLocation(int id, TrainLocation trainlocation)
{
if (ModelState.IsValid && id == trainlocation.id_trainlocation)
{
db.TrainLocation.Attach(trainlocation);
db.ObjectStateManager.ChangeObjectState(trainlocation, EntityState.Modified);

try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}

return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}

// POST api/Traininglocation
public HttpResponseMessage PostTrainLocation(TrainLocation trainlocation)
{
if (ModelState.IsValid)
{
db.TrainLocation.AddObject(trainlocation);
db.SaveChanges();

HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, trainlocation);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = trainlocation.id_trainlocation }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}

谁能帮助我?

最佳答案

我改变了我的 Controller :其中 PutTrainLocation 为 PostTrainLocation。感谢您的回复!

    public HttpResponseMessage PostTrainLocation(int id, TrainLocation trainlocation)
{
if (ModelState.IsValid && id == trainlocation.id_trainlocation)
{
db.TrainLocation.Attach(trainlocation);
db.ObjectStateManager.ChangeObjectState(trainlocation, EntityState.Modified);

try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}

return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}

关于jquery - 使用 ajax Post 编辑数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20338436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com