我有一个ajax调用web-api页面(/api/atten丹斯)。它插入一条记录,并返回OK()作为结果。
问题: 插入正确完成,并且将返回OK,但是它调用的不是。done()/success()函数,而是。fail ()/error()。
我尝试了两种方法($.ajax()和$.Post()),它们都结束了结果相同
1)
$.post("/api/attendances", { gid: button.attr("data-gid") })
.done(function () {
alert("success...");
})
.fail(function () {
alert("Something failed...");
});
服务端代码:
2)
public class AttendancesController : ApiController
{
//.........other initialize , ... codes in controller
[HttpPost]
public IHttpActionResult Attend(AttendDto dto)
{
//.....check for duplicate , ...
var attendanceModel = new Attendance
{
AttendeeId = userId,
gId = dto.gId
};
_context.Attendances.Add(attendanceModel);
_context.SaveChanges();
return Ok();
}
}
3) 定义DTO
public class AttendDto
{
public int gId { get; set; }
}
我认为它在SaveChanges方法上抛出错误。您可以通过注释Save调用来验证,然后检查api是否转到fail函数。