3

I have an ajax call to a web-api page (/api/attendances). Its inserting a record , and return OK() as a result.

Issue: insert is done correctly ,and OK will be returned, but instead of .done()/success() function, it calls .Fail()/error() .

I tried it in two ways ($.ajax() and $.Post()), both of them ends up with same result

1)

  $.post("/api/attendances", { gid: button.attr("data-gid") })
          .done(function () {
               alert("success...");
              })
          .fail(function () {
              alert("Something failed...");
              });

ServerSide Code:

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) Defining DTO

  public class AttendDto
    {
        public int  gId { get; set; }
    }

Any help would be appreciated.

Sara N
  • 1,079
  • 5
  • 17
  • 45
  • Sounds like an exception is occuring in the controller. Add .fail(funtion(error){ console.log(error);} to the Ajax funtion and display the messege. – Marcus Höglund Aug 08 '16 at 06:19
  • @MarcusH thanks , it seems to be call back again , after running the first time, the error is "too much recursion" in console . – Sara N Aug 08 '16 at 07:18

1 Answers1

0

I think its throwing an error on the SaveChanges method. You can verify by commenting the Save call and then check if the api goes to the fail function.

Jofy Baby
  • 122
  • 8