我可以在JQuery中访问ColdFusion会话吗?

Vai*_*esh 1 coldfusion jquery

我有一个ColdFusion页面用于登录.单击登录按钮由JQuery函数处理.身份验证本身是假的,并且发生在函数本身内部.一旦成功,我将内容从另一个ColdFusion页面加载到第一页.我能以任何方式在JQuery的ColdFusion会话中设置变量吗?

Tra*_*vis 6

在最严格意义上的问题,不,jQuery/javaScript无法直接访问ColdFusion变量,Kevin B是正确的.但是,您可以使用AJAX(这是JavaScript,而不是jQuery,尽管jQuery有一些方法可以轻松实现)将数据发送到ColdFusion而无需在浏览器中进行完整的往返.这样做会导致ColdFusion 根据您选择的方法在URLFORM范围中创建变量.不幸的是,FORMURL变量只存在请求的时间,这样你就那么用ColdFusion设置任何SESSION你需要使用设置变量URL或者FORM你刚刚发送的变量.

jQuery有一些方法可以做到这一点.

一个非常简单的例子可能看起来像这个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>
Run Code Online (Sandbox Code Playgroud)

setVariable.cfm中的CF代码可能如下所示

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