我正在使用 jquery 对 Entity Framework API Controller 进行 API 调用,我正在尝试调用 Put 方法:
[ResponseType(typeof(void))]
public IHttpActionResult PutProfileIDClass(int id, ProfileIDClass profileIDClass)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != profileIDClass.id)
{
return BadRequest();
}
db.Entry(profileIDClass).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ProfileIDClassExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
但是当我通过 jQuery 进行 API 调用时,我收到此错误:405(不允许使用方法)
我做错了什么?
这是我的 API 调用
var data = {
id: result.data[0].id,
profileID: result.data[0].profileID,
taken: 'true'
};
var json = JSON.stringify(data);
$.ajax({
url: '/api/ProfileIDAPI?id=' + result.data[0].id,
type: 'PUT',
contentType: "application/json; charset=utf-8",
data: json,
success: function (results) {
}
});
最佳答案
如果你想执行 PUT
请求,你应该使用 method: 'PUT'
作为 $.ajax
调用的一部分:
$.ajax({
url: '/api/ProfileIDAPI?id=' + result.data[0].id,
method: 'PUT',
contentType: "application/json; charset=utf-8",
data: json,
success: function (results) {
}
});
关于javascript - jQuery API 调用 Entity Framework API Put 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45722185/