Friday, April 5, 2013

Standard responses with Response.cfc

Standardizing responses is useful so you know what to expect when you make a service call. That is why I wrote Response.cfc (github repo link).

This response object by default returns a json packet so it can be easily manipulated by AJAX and remote API requests.  For non-AJAX/non-remote calls, I like to deserialize the json immediately after getting the response.

Properties of Response.cfc are below with sample use cases.

  • data (used to pass back context data)
  • errorCode (used to pass a unique code)
  • errorDetail (used for debugging)
  • errorMessage (used for generic messaging back to the user)
  • errors (each error to report back to the user)
  • statusCode (http status code for use by remote calls)
  • statusText (http status text for use by remote calls)
  • success (boolean flag for overall status of request)

Credit to Ben Nadel's Building An AJAX, jQuery, And ColdFusion Powered Application post for inspiring this.

No comments:

Post a Comment