Vaishak Suresh
Vaishak Suresh

Reputation: 5845

Can I access ColdFusion session inside JQuery?

I have a ColdFusion page for login. The click of the login button is being handled by a JQuery function. The authentication itself is fake and is happening inside the function itself. Once that is successful, I load contents from another ColdFusion page into the first page. Can I, in any way, set a variable in the ColdFusion session from JQuery?

Upvotes: 1

Views: 3473

Answers (2)

genericHCU
genericHCU

Reputation: 4446

in the strictest sense of the question, no, jQuery/javaScript can't access ColdFusion variables directly, Kevin B is correct. However, you can use AJAX (which is JavaScript, not jQuery, although jQuery has a few methods to make it easy) to send data to ColdFusion without having to do a full round trip in the browser. Doing so causes ColdFusion to create variables in the URL and FORM scopes depending on the method you choose. Unfortunately, FORM and URL variables only exist for the duration of the request so you would then use ColdFusion to set whatever SESSION variables you needed to set using the URL or FORM variables you just sent.

jQuery has a few methods to do this.

A very simple example of this may look like this jQuery:

<script>
   var myName = "Travis";
   $.get('setVariable.cfm?someVar='+myName, // Send a value to the server in the URL.
      function(data){ // tell the user what the server said (optional).
         alert(data);  //data is whatever was returned by the server.
      }
   );   
</script>

CF code in setVariable.cfm might look like

<cftry>
    <cfset session.userName = url.someVar>
    Session user was set.
    <cfcatch>
        <cfoutput>
            Oh, Crap! Something bad happened! (#cfcatch.message#)
        </cfoutput>
    </cfcatch>
</cftry>

Upvotes: 6

Kevin B
Kevin B

Reputation: 95022

No, all you have access to on the client side is the session/client token cookies. You can however set the value of a custom cookie then access that cookie with coldfusion.

Upvotes: 4

Related Questions