1

I'm issuing a PATCH request to my server in order to update a title:

$.ajax({
    url: Settings.get('serverURL') + 'Playlist/UpdateTitle',
    type: 'PATCH',
    dataType: 'json',
    data: {
        id: model.get('id'),
        title: title
    },
    success: function () {
        console.log("Success!");
    },
    error: function (error) {
        console.error("Failure!");
    }
});

[Route("UpdateTitle")]
[HttpPatch]
public IHttpActionResult UpdateTitle(PlaylistDto playlistDto)
{
    using (ITransaction transaction = Session.BeginTransaction())
    {
        PlaylistManager.UpdateTitle(playlistDto.Id, playlistDto.Title);

        transaction.Commit();
    }

    return Ok();
}

Everything works great except for the fact that the ajax request's error callback is executed and not the success callback.

Before Web API 2, I was using the following method which did not have the problem. Clearly the issue is that I am returning an Ok result instead of a JSON success object:

[HttpPost]
public JsonResult UpdateTitle(Guid playlistId, string title)
{
    using (ITransaction transaction = Session.BeginTransaction())
    {
        PlaylistManager.UpdateTitle(playlistId, title);

        transaction.Commit();
    }

    return Json(new
        {
            success = true
        });
}

What's the proper way to indicate success with web API 2?

Sean Anderson
  • 27,963
  • 30
  • 126
  • 237

2 Answers2

5

The problem is actually pretty simple:

dataType: 'json',

tells jQuery to not use intelligent guessing for the server's response type. It will throw an error if the server does not respond with json. If I just delete that property then everything works as expected.

Sean Anderson
  • 27,963
  • 30
  • 126
  • 237
4

Return an HttpResponseMessage and set the StatusCode to OK:

public HttpResponseMessage UpdateTitle() {
    ...
    return Request.CreateResponse(HttpStatusCode.OK);
}    
Jason
  • 2,271
  • 2
  • 24
  • 23
Neil Smith
  • 2,565
  • 1
  • 15
  • 18
  • But I think it's more appropriate in Web API 2 to use IHttpActionResult. See this thread: http://stackoverflow.com/questions/21758615/why-should-i-use-ihttpactionresult-instead-of-httpresponsemessage Oh, actually, your solution doesn't resolve the issue unfortunately. :( – Sean Anderson Mar 14 '14 at 20:22
  • Oh wow, I wasn't aware of the new HttpResponseMessage abstraction. I thought the Ok() was something you wrote. I'd assume their OK() is just doing the same as my answer. If you're still hitting your error callback, I'd guess the problem isn't with the api but with the client side. Hit your action with fiddler and check out the response - you should be getting a 200 OK if you're returing Ok(). – Neil Smith Mar 14 '14 at 20:33
  • 1
    It was a mistake with my javascript. I shouldn't have been indicating I was expecting a json response from the server when it's not sending any data back. :) Easy peasy! – Sean Anderson Mar 14 '14 at 20:34
  • Ahh - never would've guessed. Nice catch though. – Neil Smith Mar 14 '14 at 20:35