UPDATE2:
这里包含了jQuery(以及当前的jQuery):
<script type="text/javascript" src="/honors/thesis_submission/js/jquery-ui.js"></script>
$(document).ready(function() {
$('#advisor_email').autocomplete({source: "/honors/thesis_submission/cfc/advisors.cfc?method=advisorLookUp&returnFormat=json", minLength: 2});
});
更新方法:
<cffunction name="advisorLookUp" access="remote" output = "false" returntype="any">
<cfargument name="term" type="string" required="no">
<cfset var advisorLookUp = "">
<cfset var a = []>
<cfset var s = {}>
<cfquery name = "advisorLookUp" datasource = "#dsn#">
SELECT id, email
FROM budPerson
WHERE email like <cfqueryparam cfsqltype="cf_sql_varchar" value = "#trim(arguments.term)#%">
</cfquery>
<cfloop query = "advisorLookUp">
<cfset s = StructNew()>
<cfset s["id"] = id>
<cfset s["label"] = email>
<cfset s["value"] = email>
<cfset arrayAppend(a,s)>
</cfloop>
<cfreturn a>
</cffunction>
形式:
<cfform enctype="multipart/form-data" name = "coversheet">
<!-- other fields excluded -->
<input name="advisor_email" type="text" id="advisor_email" size="40">
<!-- other fields excluded -->
</cfform>
请再次注意......我能够通过将我方法中的SAME代码放在常规cfm页面上并且只是cfoutputing来实现这一点......很奇怪? :\我想通过我的cfc中的远程方法来实现它。
更新
切换到jQuery UI并更新我的代码以匹配它。我仍然没有从我的方法远程获得响应。
-
我正在尝试设置jQuery自动完成插件(具体为:http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/)。
我只是在自动完成中使用coldfusion构建,但它对我不起作用(给我一个错误,显然我无能为力。)
无论如何,我的cfc中的远程方法不会给我回复。萤火虫的行为就像是好事和东西,但实际上并没有给我回报。
这是我的功能:
<cffunction name="advisorLookUp" access="remote" returntype="any">
<cfargument name="q" type="string" required="yes">
<cfset var advisorLookUp = "">
<cfset var arr = "">
<cfquery name = "advisorLookUp" datasource = "#dsn#">
SELECT id, email
FROM budPerson
WHERE email like <cfqueryparam cfsqltype="cf_sql_varchar" value = "#trim(arguments.q)#%">
</cfquery>
<cfsavecontent variable="arr">
<cfoutput query = "advisorLookUp">
#advisorLookUp.email# | #advisorLookUp.id#
</cfoutput>
</cfsavecontent>
<cfreturn arr>
</cffunction>
我按照插件的方式格式化了返回。好吧,我觉得这并不重要......我真的只是想知道我得到了回应(我现在不是这样)。
这是我的jQuery调用方法:
$('#advisor_email').autocomplete(
"/honors/thesis_submission/cfc/advisors.cfc?method=advisorLookUp&returnFormat=json");
我只是通过调用来测试没有jQuery的方法,它运行得很好。有什么想法吗?
答案 0 :(得分:5)
您正在运行哪个版本的ColdFusion?如果不是最新的(版本9),那么您可能需要将以下if语句添加到Application.cfc中的 onRequestStart()方法,以解决onRequest()函数的存在的错误与远程电话混淆:
<cffunction name="onRequestStart" returnType="boolean" output="false">
<cfargument name="thePage" type="string" required="true">
<!--- Other code in your onRequestStart method --->
<!--- Add the following to the end of your onRequestStart method --->
<cfif ListLast( arguments.thePage,"." ) IS "cfc">
<cfset StructDelete( this, "onRequest" )>
<cfset StructDelete( variables,"onRequest" )>
</cfif>
<cfreturn true>
</cffunction>
检测请求是否远程cfc调用并删除onRequest函数。
(注意:确保“arguments.thePage”匹配您为该参数声明的任何名称。有些人将其命名为TargetPage或类似名称。只要它与您声明的名称相匹配就无所谓。)
答案 1 :(得分:0)
在与此摔跤之后......我决定只在某个页面(ajax.cfm)上将coldfusion代码放在我的方法中并以这种方式进行远程调用。它现在有效......但是,如果有人知道如何通过远程调用cfc中的方法来使用它,我会更高兴。
<cfparam name="url.term" default = "">
<cfset a = []>
<cfset s = {}>
<cfquery name = "advisorLookUp" datasource = "#dsn#">
SELECT id, email
FROM budPerson
WHERE email like <cfqueryparam cfsqltype="cf_sql_varchar" value = "#trim(url.term)#%">
</cfquery>
<cfloop query = "advisorLookUp">
<cfset s = StructNew()>
<cfset s["id"] = id>
<cfset s["label"] = email>
<cfset s["value"] = email>
<cfset arrayAppend(a,s)>
</cfloop>
<cfoutput>#serializeJSON(a)#</cfoutput>
答案 2 :(得分:0)
正如Tentonaxe所提到的,将returnformat设置为json,CF将负责序列化并以看起来正确的格式返回结构(至少对于自动完成的JQuery版本)
答案 3 :(得分:0)
我假设你在调用页面中的jqueryui之前包含了jquery。无论如何这是有效的,在testpage上将其设置回cfc的路径
testpage.cfm
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
</head>
<body>
<cfform name = "coversheet">
<!-- other fields excluded -->
<input name="advisor_email" type="text" id="advisor_email" size="40">
<!-- other fields excluded -->
</cfform>
<script>
$(document).ready(function() {
$('#advisor_email').autocomplete({source: "cfcs/advisors.cfc?method=advisorLookUp&returnFormat=json", minLength: 2});
});
</script>
</body>
</html>
advisors.cfc
<cfcomponent>
<cffunction name="advisorLookUp" access="remote" output = "false" returntype="any">
<cfargument name="term" type="string" required="no">
<cfset var advisorLookUp = "">
<cfset var a = []>
<cfset var s = {}>
<!--- no dsn so hardcode some data --->
<cfset hardcodeData = queryNew("id, email")>
<cfset queryAddRow(hardcodeData)>
<cfset querySetCell(hardcodeData, "id", 1)>
<cfset querySetCell(hardcodeData, "email", "petra@xyz.com")>
<cfset queryAddRow(hardcodeData)>
<cfset querySetCell(hardcodeData, "id", 2)>
<cfset querySetCell(hardcodeData, "email", "pete@abc.com")>
<cfset queryAddRow(hardcodeData)>
<cfset querySetCell(hardcodeData, "id", 3)>
<cfset querySetCell(hardcodeData, "email", "peter@abc.com")>
<!--- now spoof the query --->
<cfquery name = "advisorLookUp" dbtype="query">
SELECT id, email
FROM hardcodeData
WHERE email like <cfqueryparam cfsqltype="cf_sql_varchar" value = "#trim(arguments.term)#%">
</cfquery>
<cfreturn advisorlookup>
</cffunction>
</cfcomponent>
在输入框中输入“pe”会在firebug响应中生成此内容
{ “列”:[ “ID”, “电子邮件”], “DATA”:[[ “1.0”, “petra@xyz.com”],[ “2.0”, “pete@abc.com”] ,[ “3.0”, “peter@abc.com”]]}
是的,返回查询,如果你想要以不同的方式格式化返回,你可以摆弄
就像你拥有它一样
<cfloop query = "advisorLookUp">
<cfset s = StructNew()>
<cfset s["id"] = id>
<cfset s["label"] = email>
<cfset s["value"] = email>
<cfset arrayAppend(a,s)>
</cfloop>
<cfreturn a>