Jquery 使用AJAX调用ColdFusion函数
当用户单击链接时,我需要调用ColdFusion函数(存在于.cfm文件中)。我想使用jQuery来实现这一点。我有一个jQuery代码片段-Jquery 使用AJAX调用ColdFusion函数,jquery,ajax,coldfusion,Jquery,Ajax,Coldfusion,当用户单击链接时,我需要调用ColdFusion函数(存在于.cfm文件中)。我想使用jQuery来实现这一点。我有一个jQuery代码片段- <script type="text/javascript"> $(document).ready(function(){ $("td.ViewLink a").click(function(event){ event.preventDefault(); )} $(文档).ready(函数(){ $(“t
<script type="text/javascript">
$(document).ready(function(){
$("td.ViewLink a").click(function(event){
event.preventDefault();
)}
$(文档).ready(函数(){
$(“td.ViewLink a”)。单击(函数(事件){
event.preventDefault();
)}
我对jQuery和AJAX都不熟悉,所以我在这里可能听起来很幼稚。我应该使用AJAX调用ColdFusion函数吗?类似于请求在服务器上执行特定函数
在此方面的任何帮助都将不胜感激
干杯。您不一定需要使用“AJAX”(特别是XML部分),但可以使用远程服务器调用:
$.get('/execute-function.cfm?func=whatever', function (result) { $('#result').html(result); });
这取决于你需要对结果做什么。上面的代码将把HTML结果放在你页面上的一个div中:
<div id="result"></div>
您可以使用异步调用和解析XML,但我发现我很少需要这两种方法。如果您的cfm中有多个函数(即使没有),请将它们放在cfc中。 然后,您可以使用以下url模式来调用特定的方法 名为myEntityWS.cfc的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>
您不能完全按照示例代码中的要求来做。不过,您有一些选择 方法1:远程访问对象 将您的函数移动到CFC中,并通过CFC的URL访问它们。此访问方法要求函数使用权限属性
access='remote'
——如果设置为public(默认)或private(或package,或任何角色级别等),则在尝试远程访问它时,您将收到一个method not found错误
这样做,您将创建一个SOAP Web服务并通过AJAX使用它
http://domain.com/path/to/your.cfc?method=functionName&argument1=arg1Val&foo=bar&...
如果您有ColdFusion 8,还可以指定returnFormat='format'
url参数,该参数将动态地将返回的任何本地ColdFusion数据对象转换为请求的格式。它支持JSON、XML和WDDX
foo.cfc
方法2:远程代理对象 方法#1的负面影响是实例化CFC的效率受到了轻微的影响,因此,如果这个特定的AJAX方法将非常频繁地运行,并且/或者您的CFC包含多个方法或者超过几百行,那么您不希望为每个请求反复实例化它在中,您缓存了实现应用程序范围中的功能的CFC,并且有一个单独的“远程代理”CFC,它的重量要轻得多,只是充当http请求和缓存的CFC之间的代理(因此命名) 在这种模式中,您的业务对象(具有执行实际工作的功能的对象)可以具有
access=public
(或包等),只要代理可以访问它。但是代理本身必须具有access=remote
proxy.cfc
方法3:自己动手 最后,您可以手动实现函数调用并在CFM模板中返回编写CFC会对性能造成影响,但会对您造成更多的键入和其他潜在的故障点。为此,请将您的函数包含在CFM模板中,并将输出流视为返回到浏览器的文本流
您应该小心地管理返回值中的空白(使用<代码>输出= false <代码>函数定义,考虑使用<代码>
刚刚看到这个帖子。我使用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>
也许,它会对其他人有用…如果您愿意,可以尝试使用CF8中的
标记。在JavaScript中使用ColdFusion变量非常强大!
一定要使用
<cfoutput> var #toScript(ColdFusionVAR, 'javascriptVar')# </cfoutput>
在JavaScript端使用类
你希望我们这样做
var JS_CFC_Obj;
JS_CFC_Obj = new jsCFCclassName()
您现在可以调用该cfc内部的函数
jsCFCclassName.functionName(javascript var);
请参阅下面razzed的回答。在稍微高一点的级别上,您基本上只是发出http请求并对结果进行处理。您可以对StackOverflow主页进行相同的调用,并获得浏览器所需的HTML,但您可以调用自定义Coldfusion页面,并返回该页面所返回的内容您可以将其保留为HTML,或者让Coldfusion返回XML、JSON甚至纯文本。
$.ajax()
是$.get()的一个很好的替代方法
因为它给了你更多的选择,但它们都能工作。我知道这已经有将近十年的历史了,但是你能解释一下为什么要使用.cfc而不是.cfm文件吗?@tim_stuff如果我没记错的话,ColdFusion将自动生成一个围绕cfc:wsdl等的web服务。这在cfm中是不会发生的。
http://domain.com/path/to/proxy.cfc?method=foobar&arg1=some%20value&returnFormat=JSON
<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>
<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>
$.ajax({
type:"POST",
url:"financeTerms.cfc?method=getFinanceTerms",
data: "sales_price=55000&interestRate=5.99",
cache:false,
success: function(msg) {
$("#someDiv").html(msg);
}
});
<cfoutput> var #toScript(ColdFusionVAR, 'javascriptVar')# </cfoutput>
<head>
<cfajaxproxy cfc="cfc.yourclassname" jsclassname="jsCFCclassName">
</head>
var JS_CFC_Obj;
JS_CFC_Obj = new jsCFCclassName()
jsCFCclassName.functionName(javascript var);