使用AJAX调用ColdFusion函数

Arn*_*shn 13 ajax coldfusion jquery

当用户点击链接时,我需要调用ColdFusion函数(存在于.cfm文件中).我想用jQuery来做.我有一个jQuery片段,看起来像 -

<script type="text/javascript">
$(document).ready(function(){
       $("td.ViewLink a").click(function(event){
         event.preventDefault();

)}
Run Code Online (Sandbox Code Playgroud)

我是jQuery和AJAX的新手,所以我可能听起来很天真.我应该使用AJAX来调用ColdFusion函数吗?类似于请求在服务器上执行特定功能的东西.

在这方面的任何帮助表示赞赏.

干杯.

ant*_*upe 17

如果您的cfm中有多个功能(即使您没有),请将它们放在cfc中.然后,您可以使用以下url模式来调用特定方法.

cfc名为myEntityWS.cfc

<cfcomponent>
  <cffunction name="updateDescription" access="remote" returntype="string">
    <cfargument name="value" type="string" required="yes">
    <cftry>
      your code here
    <cfcatch>
      <cfoutput>
        #cfcatch.Detail#<br />
        #cfcatch.Message#<br />
        #cfcatch.tagcontext[1].line#:#cfcatch.tagcontext[1].template#
      </cfoutput>
    </cfcatch>
    </cftry>
  </cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)

使用Javascript

$.get('myEntityWS.cfc?method=updateDescription&value=someValue');
Run Code Online (Sandbox Code Playgroud)

  • @tim_stuff如果我没记错的话,ColdFusion会自动围绕cfc:wsdl等生成一个Web服务.这种情况不会发生在cfm中. (3认同)

Ada*_*tle 16

你不能完全按照你在示例代码中尝试的那样做.不过,你有几个选择.

方法1:远程访问对象

将您的功能移动到CFC中,并通过CFC的URL访问它们.此访问方法要求该函数使用权限属性access='remote'- 如果设置为公共(默认)或私有,(或包,或任何角色级别等),那么在尝试访问它时,您将获得找不到方法的错误远程.

这样做,您将创建一个SOAP Web服务并通过AJAX使用它.您可以通过在jQuery请求中使用以下格式来执行此操作:

http://domain.com/path/to/your.cfc?method=functionName&argument1=arg1Val&foo=bar&...
Run Code Online (Sandbox Code Playgroud)

如果你有ColdFusion 8,你也可以指定returnFormat='format'url参数,它会将您返回的任何本机ColdFusion数据对象转换为动态请求的格式.它支持JSON,XML和WDDX.

foo.cfc

<cfcomponent output="false">
  <cffunction name="foobar" output="false" access="remote" hint="...">
    <cfargument name="arg1" type="string" required="true" />
    ...
    <cfreturn someVar />
  </cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)

通过URL访问:

http://domain.com/path/to/foo.cfc?method=foobar&arg1=some%20value&returnFormat=JSON
Run Code Online (Sandbox Code Playgroud)



方法2:远程代理对象

方法#1的负面影响是实例化CFC会有轻微的效率,因此如果这种特殊的AJAX方法将非常频繁地运行,和/或您的CFC包含多个方法或者长于几百行,您不希望为每个请求反复实例化它.相反,您可能希望查看远程代理模式,其中缓存实现应用程序范围中的功能的CFC,并且具有单独的"远程代理"CFC,其重量轻得多,并且仅用作代理(因此http请求和缓存的CFC之间的名称.

在此模式中,access=public只要代理可以访问您的业​​务对象(具有执行实际工作的功能的对象),就可以拥有(或打包等).但是,代理本身必须具备access=remote.

proxy.cfc

<cfcomponent output="false">
  <cffunction name="foobar" output="false" access="remote" hint="...">
    <cfargument name="arg1" type="string" required="true" />
    <!--- Application.foo is an instantiated object of foo.cfc --->
    <cfreturn Application.foo.foobar(argumentCollection=arguments) />
  </cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)

通过URL访问:

http://domain.com/path/to/proxy.cfc?method=foobar&arg1=some%20value&returnFormat=JSON
Run Code Online (Sandbox Code Playgroud)



方法3:自己动手

最后,您可以手动实现函数调用并在CFM模板中返回.这种方法不涉及编写CFC的(轻微)性能损失,但会为您打字更多,以及其他潜在的失败点.为此,请在CFM模板中包含您的函数,并将输出流视为:将返回到浏览器的文本流.

您应该小心管理返回值中的空格(使用output=false函数定义,考虑使用<cfsetting enableCFOutputOnly='true',并且只需要小心整体间距).如果您的jQuery请求需要JSON,则需要对其进行序列化.(如果需要在ColdFusion 6或7 上将数据序列化为JSON,我建议使用JSONUtil)

使用这种方法,您将AJAX请求指向带有URL参数的.cfm文件,然后您需要编写获取这些url参数的代码并将它们传递给函数,然后显示(实质上,返回到AJAX请求)功能的结果.

foo.cfm

<cfsetting enableCFOutputOnly="true">
<cfparam name="arg1" default="defaultVal"/>

<cffunction name="foobar" output="false" access="remote" hint="...">
  <cfargument name="arg1" type="string" required="true" />
  ...
  <cfreturn someVar />
</cffunction>

<cfset variables.result = foobar(url.arg1) />
<cfoutput>#serializeJSON(variables.result)#</cfoutput>
Run Code Online (Sandbox Code Playgroud)


CFN*_*nja 8

刚刚看到这篇文章.我使用cfc和jquery ajax来显示一堆计算值.我的cfc有以下内容:

<cfcomponent output="true">
<cfscript>
    this.init();
</cfscript>
     <cffunction name="init" access="public" returntype="any">
       <cfset variables.dsn = application.dsn>
        <cfreturn variables.dsn> 
     </cffunction>
     <cffunction name="getFinanceTerms" access="remote" output="true" returntype="void">
         <cfargument name="sales_price" type="numeric" required="yes">
         <cfargument name="interestRate" type="numeric" required="yes">
           <!--- some calculations here --->
         #arguments.salesPrice# <!--- just to have something displayed --->
         <cfreturn>
     </cffunction>
 </cfcomponent>
Run Code Online (Sandbox Code Playgroud)

我使用JQuery.ajax:

  $.ajax({
      type:"POST",
      url:"financeTerms.cfc?method=getFinanceTerms",
      data: "sales_price=55000&interestRate=5.99",
      cache:false,
      success: function(msg) {
      $("#someDiv").html(msg);
      }
  });
Run Code Online (Sandbox Code Playgroud)

也许,它会对其他人有用......