I have a template set up that I'm making ajax calls to via jQuery. I'm passing a URL parameter called "method" and then doing a <cfswitch> through each method to determine what block of code to execute.
Problem is, I don't want this page to be accessible outside of the ajax call. In other words, I don't want the template to run if someone just types the URL into their browser.
Is there a way to do that? I thought in .php there was a way to tell what type of request it was. Does anything like this exist in Coldfusion? Or any suggestions?
jQuery injects the request with a X-Requested-With
header set with the value "XMLHttpRequest". In coldfusion, you can view this by dumping the HTTP Request:
<cfdump var="#getHTTPRequestData()#">
So, all you need to do is test for that header, for example:
<cfset reqData = getHTTPRequestData()>
<cfif structKeyExists(reqData.headers,"X-Requested-With") and reqData.headers["X-Requested-With"] eq "XMLHttpRequest">
Got an ajax request
<cfelse>
<!--- do something else, or nothing --->
</cfif>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With