12

I have an ajax post as such:

$.post("/api/v1/payment_methods/create_credit_card", values)
.done (response) ->
  console.log("GOOD JOB")
.fail (response) ->
  console.log("Adas")

The response is a 201, however, done doesn't seem to be capturing it and instead it's going to fail. I thought 201 would of been considered a success and would of been captured by done. Any ideas of why it wouldn't be working?

Note: The above code is in coffeescript, which doesn't really affect the question but explains my syntax

adeneo
  • 312,895
  • 29
  • 395
  • 388
JustNeph
  • 761
  • 3
  • 10
  • 25
  • Well I wasn't originally sending it myself. The api I was posting to would send back a string if it was a success which got bundled as a 201. Is it better form to just return a 200 manually then? – JustNeph Feb 20 '13 at 20:01
  • 1
    Does it work if you change it to 200? – Keith Rousseau Feb 20 '13 at 20:06
  • I have tried 200 and still no good. The status check in ajax picks it up but it still fails – JustNeph Feb 20 '13 at 20:36

2 Answers2

8

So we figured out what was wrong, JSON.parse was throwing a Syntax Error - so the values sent in isnt in a valid JSON format. The poster wasnt able to see the Syntax error in chrome, but firebug showed the error.

This indicates that whenever Javascript will throw an exception, the response might still be 200, 201, 202 etc. - but because of the syntax error the fail() function would be triggered.

EDIT - Also you should probably use a different response, many use 200 - OK, but Id recommend to use 202 - ACCEPTED in this case.

am_
  • 2,378
  • 1
  • 21
  • 28
  • Oh sorry, I forgot to mention my above code is in Coffeescript – JustNeph Feb 20 '13 at 20:00
  • I tried changing it to return a 202 but fail still seems to capture it – JustNeph Feb 20 '13 at 20:06
  • Have you used firebug and looked at the response? – am_ Feb 20 '13 at 20:07
  • Yes, I'm viewing the response in the chrome developer tool. Calling response.status returns a 202(was a 201 but I changed it based on previous comments) and response.statusText is "Accepted". – JustNeph Feb 20 '13 at 20:08
  • And you are sure this isnt caused by a hiccup when compiling coffescript into javascript? Might want to try the direct javascript just in case. – am_ Feb 20 '13 at 20:10
  • Yea, the actual code that displays on the page is: return $.post("/api/v1/payment_methods/create_credit_card", values).done(function(response) { return console.log("GOOD JOB"); }).fail(function(response) { return console.log("Adas"); }); – JustNeph Feb 20 '13 at 20:13
  • Ok that looks valid, what version of jQuery are you using? – am_ Feb 20 '13 at 20:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24851/discussion-between-am-and-justneph) – am_ Feb 20 '13 at 20:18
  • jQuery has [special handling for 204 NO CONTENT and 304 NOT MODIFIED](https://github.com/jquery/jquery/blob/master/src/ajax.js#L706) where even if the parsed response errors out because of improper JSON, it will still consider the request successful. Therefore, any status codes <400 besides these two that have improper JSON (or no content in the response) will fail. – Sean Adkinson Aug 20 '14 at 22:50
3

Looking at the source, success should fire for anything between 200 - 300 and 304. An alternative is to explicitly call out the statusCode:

$.ajax({
  statusCode: {
    201: function() {
      console.log("HERE");
    }
  }
});
Keith Rousseau
  • 4,435
  • 1
  • 22
  • 28
  • I could manually check for a specific status code but I wanted to use the done method to check for all successes. Is there any reason why done wouldn't fire for a 201 or 202? – JustNeph Feb 20 '13 at 20:07
  • Like I said, based on the source it appears that done should capture it. I was just suggesting the statusCode callback to see if that captured it correctly. – Keith Rousseau Feb 20 '13 at 20:07