Need to determine if a request is an AJAX request or a standard POST/GET request?
Often times you might want to include a partial view to be sent to the browser when the request is not an AJAX request. For example, the header and footer of a page should only be returned for a full page state change. To accomplish this task is ColdFusion is really quite simple.
If you are using a Javascript library such as prototype.js it is even easier because prototype appends two custom lines to the HTTP request that is sent by the browser:
- X-Requested-With, and
- X-Prototype-Version.
Combining this with the GetHttpRequestData() function in ColdFusion you get:
1 2 3 4 5 6 7 8 9<cffunction name="isAJAX" output="false" returntype="boolean" access="public"> <cfset var headers = GetHttpRequestData().headers> <cfif structkeyexists(headers, "X-Requested-With")> <cfif headers['X-Requested-With'] eq 'XMLHttpRequest'> <cfreturn true> </cfif> </cfif> <cfreturn false> </cffunction>
Its really that simple…

No Comments, Comment or Ping
Reply to “Determining if a request is AJAX”