Does jquery success function assume status code 200?

28
October 22, 2019, at 6:20 PM

In an ajax success function, is it assumed the status code is 200? or could it be successful if you return 304 or 503?

What actually defines it's successful?

Answer 1

You're correct, jQuery ajax will "fail" if it receives an HTTP 503 ... or 500, or 404, etc. It will also (obviously) fail on ECONNREFUSED or any socket-level (communications) error.

Here is the documentation:

https://api.jquery.com/jquery.ajax/

jQuery.ajax(): Perform an asynchronous HTTP (Ajax) request.

jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});

An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.

https://api.jquery.com/deferred.fail/

deferred.fail(): Add handlers to be called when the Deferred object is rejected.

It's worth noting that the connection can succeed ... the server can return HTTP 200/OK ... and your jQuery Ajax call can STILL trigger a "fail".

For example, if you're expecting a JSON response, and the server returns XML:

https://www.mkyong.com/jquery/jquery-ajax-request-return-200-ok-but-error-event-is-fired/

ADDITIONAL INFO:

  • To answer your original question: Yes, "success" generally implies an HTTP 200.

  • But a lot of different things besides a non-HTTP 200 can trigger a "fail".

  • You can even have an HTTP 200 and still "fail" (per the link above).

  • Or have a non-HTTP 200 and still "succeed" (for example, HTTP 201).

  • HTTP 304, is a "special case". Look here or here.

Q: Is your question answered?

READ ALSO
How to optimize “ScrollMagic+TweenMax+GSAP” performance?

How to optimize “ScrollMagic+TweenMax+GSAP” performance?

At first sight an easy animation causes lags and is choppy on mobile devicesIt looks quite normal on PC resolution, but not on mobile

22
How to update sharepoint list item with selected options from mcq form

How to update sharepoint list item with selected options from mcq form

I have a mcq form developed using html and jqueryThe form is deployed in a sharepoint sitepage

17
GET request from 2 APIs in one function (JQuery)

GET request from 2 APIs in one function (JQuery)

I'm trying to build a page based on CoinGecko APIsI was asked to make two different GET request using JQuery, one without parameter, and one with id parameter, but I can't write everything correctly with the second API

29