Skip to content Skip to sidebar Skip to footer

Error Checking In Ajax Response

I am getting a an object from my API call, i am trying to do an error checking before rendering the JSON data to my view. function response(oResponse) { if(oResponse &

Solution 1:

You can probably simplify the condition to oResponse && oResponse.responseCode if all you care about is that it is not null nor undefined, and it has a responseCode property that is not null nor undefined.

  • The check for oResponse != null is redundant since null evaluates to false.
  • The check for oResponse.responseCode !== null is redundant for the same reason.
  • The check for typeof oResponse === "object" is redundant since if it was a string or other type it wouldn't have a responseCode property.

Note that if oResponse.responseCode === 0 or another "falsy" value, this condition will evaluate to false (i.e., that is considered an "invalid" responseCode).

@snowYetis comment about an error handler is also a good idea to have in there in case there is an error on the server. It won't cover the validation of a successful response that is invalid though.


Post a Comment for "Error Checking In Ajax Response"