Skip to content

Instantly share code, notes, and snippets.

@ivanionut
Forked from JamoCA/isAjaxRequest.cfm
Last active August 29, 2015 14:16
Show Gist options
  • Save ivanionut/10fbb836fccdc47ecfff to your computer and use it in GitHub Desktop.
Save ivanionut/10fbb836fccdc47ecfff to your computer and use it in GitHub Desktop.
This ColdFusion UDF will query the server's request headers to determine if the request is an Ajax form post from jQuery. (jQuery adds a special header to all ajax requests.)
<cfscript>
function isAjaxRequestPost(){
var headers = getHttpRequestData().headers;
return CGI.Request_Method is "POST" and StructKeyExists(headers, "X-Requested-With") AND (headers["X-Requested-With"] EQ "XMLHttpRequest");
}
</cfscript>
<cfif not isAjaxRequestPost()>
<!--- log attempt, alert admin, etc --->
<cfheader statuscode="403" statustext="Forbidden">
<cfcontent type="text/html; charset=UTF-8"><cfoutput>Forbidden</cfoutput><cfabort>
</cfif>
<!--- perform database update, modify cookies, upload files, etc --->
<cfcontent type="application/json; charset=UTF-8"><cfoutput>{"status":1}</cfoutput><cfabort>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment