Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make .cfm template accessible only via ajax call?

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?

like image 625
jyoseph Avatar asked Dec 10 '22 19:12

jyoseph


1 Answers

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>
like image 65
karim79 Avatar answered Dec 29 '22 14:12

karim79