<!--- Kill extra output. ---> <cfsilent> <!--- Create the default response object. ---> <cfset REQUEST.Response = StructNew() /> <!--- This is the flag for API success. ---> <cfset REQUEST.Response.Success = true /> <!--- This is a message that may or may not be populated by the request API. ---> <cfset REQUEST.Response.Message = "" /> <!--- This is the data that is being returned for the API call. ---> <cfset REQUEST.Response.Data = "" /> <!--- This is the ColdFusion component that will be used to store any API data validation errors. It is really just an array around which I have wrapped some extra functionality ---> <cfset REQUEST.FormErrors = APPLICATION.ServiceFactory.GetFormErrorCollection() /> <!--- Try to perform the API call. ---> <cftry> <!--- Param the action attribute. This is the value that will determine which method of the API is being called. ---> <cfparam name="REQUEST.Attributes.action" type="string" /> <!--- Check to see which API action we are performming for this AJAX request. ALL requests will be handled here, either explicitly or by default case. ---> <cfswitch expression="#REQUEST.Attributes.action#"> <!--- .... API CODE GOES HERE .... ---> </cfswitch> <!--- Catch any errors that have occurred during the AJAX request processing. ---> <cfcatch> <!--- Set the success flag to false. Since this is error we are going to assume that the overall process was not successful. ---> <cfset REQUEST.Response.Success = false /> <!--- Set the message from the error. Not every error produced by ColdFusion has a really nice message so let's check the message length. If there is no message, try to use the detail error info as the message. ---> <cfif Len( CFCATCH.Message )> <!--- Use the standard message. ---> <cfset REQUEST.Response.Message = CFCATCH.Message /> <cfelseif Len( CFCATCH.Detail )> <!--- Use the detail as the message. ---> <cfset REQUEST.Response.Message = CFCATCH.Detail /> <cfelse> <!--- Something went wrong. ColdFusion did not provide a message or a detail about the error that occurred. ---> <cfset REQUEST.Response.Message = "Unknown error occurred." /> </cfif> <!--- Set the data from the error detail. ---> <cfset REQUEST.Response.Data = CFCATCH.Detail /> </cfcatch> </cftry> <!--- ASSERT: At this point we have processed the API. We may have thrown some sort of error in the process, or the API event may have gone perfectly. Either way, we have a fully populated REQUEST.Response object at this point that we can return. ---> <!--- Let's check to see if we have any data validation errors. We are going to handle this centrally so that each API action doesn't have to handle it individually. ---> <cfif REQUEST.FormErrors.Size()> <!--- Set the success flag to false. If there are any errors in our form error collection then we will assume that the overall action was not successful. ---> <cfset REQUEST.Response.Success = false /> <!--- Set data validation message. ---> <cfset REQUEST.Response.Message = "Please check your data." /> <!--- <br> Set the validation errors as data. For this, we are going to return the array of error messages. ---> <cfset REQUEST.Response.Data = REQUEST.FormErrors.ToArray() /> </cfif> <!--- Return the response object. The response should be treated as an inline request. ---> <cfheader name="content-disposition" value="inline" /> <!--- Set the content to be plain text. Then, convert the AJAX response object to a JSON (Javascript Object Notation) string. In order to return this using the VARIABLE attribute, we must convert this string to a binary data object. ---> <cfcontent type="text/plain" variable="#ToBinary( ToBase64( REQUEST.UDFLib.AJAX.CFToJSON( REQUEST.Response ) ) )#" /> </cfsilent>