Can I access ColdFusion session inside JQuery?

我怕爱的太早我们不能终老 提交于 2019-12-24 13:56:02

问题


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?


回答1:


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.

  • jQuery.ajax() or $.ajax()
  • jQuery.post() or $.post()
  • jQuery.get() or $.get()

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>



回答2:


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.



来源:https://stackoverflow.com/questions/14511256/can-i-access-coldfusion-session-inside-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!