如何通过 AJAX 将参数传递给 CFC?
我正在使用以下脚本来调用 CFC 函数:
function loadQuery() {
$.get('QueryData.cfc',{},function(GetMyData){
$("#content").html(GetMyData)
})
return false
}
$(document).ready(function() {
$("#loadLink").click(loadQuery)
});
这是我的 HTML:
<a href="" id="loadLink">Load It</a>
<div id="content"></div>
我正在调用以下 CFC:
<cffunction name="GetMyData" access="public" returntype="query">
<cfargument name="RecordID" type="string" required="yes">
<cfset var RecordData = "">
<cfquery name="RecordData" datasource="MyDSN">
SELECT
foo.RecordID,
foo.RecordName
FROM
foo
WHERE
foo.RecordID = #ARGUMENTS.RecordID# ;
</cfquery>
<cfreturn RecordData>
问题一是当我调用 CFC 时,会显示 CFC 页面; CFC 描述出现(要求管理员通行证后)。我不想加载 QueryData.cfc;我想执行 QueryData.cfc 中的函数。
第二个问题是我无法弄清楚将参数传递给 CFC 方法的语法。
I'm using the following scrip to call a CFC function:
function loadQuery() {
$.get('QueryData.cfc',{},function(GetMyData){
$("#content").html(GetMyData)
})
return false
}
$(document).ready(function() {
$("#loadLink").click(loadQuery)
});
This is my HTML:
<a href="" id="loadLink">Load It</a>
<div id="content"></div>
I am calling the following CFC:
<cffunction name="GetMyData" access="public" returntype="query">
<cfargument name="RecordID" type="string" required="yes">
<cfset var RecordData = "">
<cfquery name="RecordData" datasource="MyDSN">
SELECT
foo.RecordID,
foo.RecordName
FROM
foo
WHERE
foo.RecordID = #ARGUMENTS.RecordID# ;
</cfquery>
<cfreturn RecordData>
Problem one is when I call the CFC, the CFC page shows up; the CFC description comes up (after asking for the Admin pass). I don't want to load QueryData.cfc; I want to execute the function inside QueryData.cfc.
The second issue is I can't figure out the syntax for passing an argument to the CFC method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 $.get 方法执行类似的操作,但我通常执行如下操作:
记录 ID 的数据存储在 DOM 中的某处,如下所示:
另外,不确定 access="public" 时它的行为方式 -它可能仍然有效 - 但它可能应该在你的函数上是 access="remote" 。
You can do something similar with the $.get method, but I usually do something like this:
Where the data for the record ID is stored somewhere in the DOM like so:
Also, not sure how it behaves with access="public" - it might still work - but it should probably be access="remote" on your function.
对于您正在做的事情,您想尝试
还是
?这容易多了。但要回答您的问题,GET url 应为
XXX.cfc?method=whatever¶m=xyz
编辑:顺便说一句,您的函数应具有
access="remote"
,并且返回 Query 对象不是一个好主意,除非您使用
。For what you're doing, would you like to try
<cfdiv>
or<cfajaxproxy>
? It's much easier.But to answer your question, the GET url should be
XXX.cfc?method=whatever¶m=xyz
edit: btw your function should have
access="remote"
, and it's not a good idea to return Query object, unless you're using<cfgrid>
.