使用 jQuery ajax 调用没有从远程 Coldfusion 方法获得返回

发布于 2024-12-03 11:26:10 字数 3197 浏览 1 评论 0原文

Update2:

这里包含 jQuery(以及当前的 jQuery):

<script type="text/javascript" src="/honors/thesis_submission/js/jquery-ui.js"></script>

域是 http://uwf.edu

$(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>

再次注意...我可以通过将我的方法中的相同代码放在常规 cfm 页面上并仅进行 cf 输出来实现此工作...很奇怪吗? :\ 我想通过我的 cfc 中的远程方法让它工作。

更新:

切换到 jQuery UI 并更新我的代码以匹配它。我仍然没有从我的方法中得到远程响应。

--

我正在尝试设置 jQuery 自动完成插件(具体来说: http://bassistance .de/jquery-plugins/jquery-plugin-autocomplete/)。

我只想在自动完成中使用 Coldfusion 构建,但它对我不起作用(给我一个显然我无能为力的错误。)

无论如何,我的 cfc 中的远程方法不会给我响应。 Firebug 表现得好像一切都很好,但实际上并没有给我回报。

这是我的功能:

<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 的情况下通过调用测试了该方法,它工作得很好。有什么想法吗?

Update2:

Here is jQuery included (along with current jQuery):

<script type="text/javascript" src="/honors/thesis_submission/js/jquery-ui.js"></script>

domain is http://uwf.edu

$(document).ready(function() {
$('#advisor_email').autocomplete({source: "/honors/thesis_submission/cfc/advisors.cfc?method=advisorLookUp&returnFormat=json", minLength: 2});
});

Updated method:

<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>

Form:

<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>

Note again...I was able to get this working by putting the SAME code that is in my method on a regular cfm page and just cfoutputing...weird much? :\ I'd like to get it working through the remote method in my cfc.

Update:

Switch to jQuery UI and updated my code to match it. I'm still not getting a response remotely from my method.

--

I'm trying to setup a jQuery Autocomplete plugin (specifically: http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/).

I would just use the coldfusion build in autocomplete, but it was not working for me (giving me an error that apparently I can do nothing about.)

Anyway, my remote method in my cfc will not give me a response. Firebug acts like it is all good and stuff, but doesn't actually give me a return.

Here is my function:

<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>

I have the return being formatted the way the plugin wants. Well, that doesn't really matter I guess...I really just want to know I'm getting a response (which I'm not at the moment).

Here is my jQuery calling the method:

$('#advisor_email').autocomplete(
     "/honors/thesis_submission/cfc/advisors.cfc?method=advisorLookUp&returnFormat=json");

I've tested the method without the jQuery by just doing a invoke and it works just fine. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦言归人 2024-12-10 11:26:10

您运行的是哪个版本的 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 或类似名称。只要它与您声明的名称匹配就没关系。)

Which version of ColdFusion are you running? If not the latest (version 9) then you may need to add the following if-statement to the onRequestStart() method in your Application.cfc to address a bug whereby the presence of the onRequest() function messes with remote calls:

<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>

This detects if the request a remote cfc call and removes the onRequest function.

(NB: Make sure "arguments.thePage" matches whatever name you have declared for that argument. Some people name it TargetPage or such like. Doesn't matter as long as it matches the name you declare.)

我最亲爱的 2024-12-10 11:26:10

经过一番思考之后......我决定将 Coldfusion 代码放入我的方法中的某个页面(ajax.cfm)上,并以这种方式进行远程调用。现在有效了。然而,如果有人知道如何让它与 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>

After wrestling with this....I decided to just put the coldfusion code in my method just on some page (ajax.cfm) and do the remote call that way. It works...now. However, if anyone knows how to get this to work with a remote call to a method in a cfc I would be much happier.

<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>
枕头说它不想醒 2024-12-10 11:26:10

正如 Tentonaxe 提到的,将 returnformat 设置为 json,CF 将负责序列化并以看起来正确的格式返回结构(至少对于 AutoComplete 的 JQuery 版本)

As Tentonaxe mentions, set returnformat to json, and CF will take care of serialising and returning your struct in what looks to be correct format (at least for the JQuery version of AutoComplete)

饮惑 2024-12-10 11:26:10

我假设您在调用页面中的 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", "[email protected]")>
        <cfset queryAddRow(hardcodeData)>
        <cfset querySetCell(hardcodeData, "id", 2)>
        <cfset querySetCell(hardcodeData, "email", "[email protected]")>
        <cfset queryAddRow(hardcodeData)>
        <cfset querySetCell(hardcodeData, "id", 3)>
        <cfset querySetCell(hardcodeData, "email", "[email protected]")>

    <!--- 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 响应

{"COLUMNS":["ID","EMAIL"]," 中生成此内容数据":[["1.0","[电子邮件受保护]"],["2.0","[电子邮件受保护]"],["3.0","[email protected]"]]}

是返回查询,如果您希望返回的格式

与您的格式不同,您可以进行修改

   <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> 

I'm assuming you include jquery before jqueryui in your calling page. Anyway this works, on testpage set it back to the path to your 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", "[email protected]")>
        <cfset queryAddRow(hardcodeData)>
        <cfset querySetCell(hardcodeData, "id", 2)>
        <cfset querySetCell(hardcodeData, "email", "[email protected]")>
        <cfset queryAddRow(hardcodeData)>
        <cfset querySetCell(hardcodeData, "id", 3)>
        <cfset querySetCell(hardcodeData, "email", "[email protected]")>

    <!--- 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>

Entering "pe" in the input box generates this in firebug responses

{"COLUMNS":["ID","EMAIL"],"DATA":[["1.0","[email protected]"],["2.0","[email protected]"],["3.0","[email protected]"]]}

yes returning the query, you could fiddle if you wanted the return formatted differently

like you had it

   <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> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文