jquery cfml ajax
这令人沮丧。我读过的任何内容都没有阐明这一点。而且事情不应该这么复杂。这是我的代码的当前状态。我一直在测试每一种组合,希望它有意义。我想要从 CF 函数返回的是记录计数并在悬停期间显示它。
$(".alphabet").hover(function () {
$.ajax({ type: "POST",
url: "cfc/basic.cfc?method=CountUsersByLetter&returnformat=json",
data: "nbw=" + $(this.target).val(),
datatype: "html",
success: function(usercount){
alert(usercount);
},
error: function (xhr, textStatus, errorThrown){
// show error
//alert(errorThrown);
console.log('errorThrown');
}
});
$(this).append($("<span>" + usercount +"</span>"));
},
function () {
$(this).find("span:last").remove();
}
);
<cffunction name="CountUsersByLetter" output="no" returntype="query" access="remote">
<cfargument name="NBW" required="false" type="string" default="A" hint="name begins with">
<cfset var qResults = "">
<cfquery name="qResults" datasource="#request.dsn#">
select count(id) as usercount from Temp_Duplicate_Individuals_CC
WHERE left(lastname,1) = <cfqueryparam value="#arguments.NBW#" />
</cfquery>
<cfreturn #qResults#>
</cffunction>
This is frustrating. Nothing I've read makes this clear. And it shouldn't be this complicated. This is the current state of my code. I've been testing every combination hoping it would make sense. All I want to return from the CFfunction is a record count and show it during the hover.
$(".alphabet").hover(function () {
$.ajax({ type: "POST",
url: "cfc/basic.cfc?method=CountUsersByLetter&returnformat=json",
data: "nbw=" + $(this.target).val(),
datatype: "html",
success: function(usercount){
alert(usercount);
},
error: function (xhr, textStatus, errorThrown){
// show error
//alert(errorThrown);
console.log('errorThrown');
}
});
$(this).append($("<span>" + usercount +"</span>"));
},
function () {
$(this).find("span:last").remove();
}
);
<cffunction name="CountUsersByLetter" output="no" returntype="query" access="remote">
<cfargument name="NBW" required="false" type="string" default="A" hint="name begins with">
<cfset var qResults = "">
<cfquery name="qResults" datasource="#request.dsn#">
select count(id) as usercount from Temp_Duplicate_Individuals_CC
WHERE left(lastname,1) = <cfqueryparam value="#arguments.NBW#" />
</cfquery>
<cfreturn #qResults#>
</cffunction>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须了解,默认情况下,ajax 本质上是异步的,因此您必须等到服务器响应才能使用仅在成功处理程序中可用的 usercount。通过将
async
设置为false
进行同步调用,或者等待服务器响应。将代码移到成功处理程序中
我不会建议您进行同步调用,因为它会使整个页面等待,有时如果服务器需要时间响应,浏览器会完全挂起。
You have to understand that
ajax
is async in nature by default so you have to wait until server responds before usingusercount
which is only available inside success handler. Either make a synchronous call by settingasync
tofalse
or wait until the server responds.Move your code inside success handler
I would not advice you to make synchronous call because it makes the whole page wait and sometimes the browser hangs completely if the server takes time to respond.
你为什么不尝试一下
cfreturn qResults.recordCount?
您现在正在返回整个查询对象。
Why don't you try
cfreturn qResults.recordCount?
You are returning the entire query object right now.
有很多东西可以在代码中进行调整。
我会在 .ajax() 调用之前添加这行代码:
@Jason Tabler 的响应很接近。使用 COUNT() 的查询的 RecordCount 将始终返回值 1(一)。我会将 cfreturn 语句更改为:
cfreturn 中不需要井号,这将返回值而不是查询对象。
ajax 调用不应该是“POST”。 “POST”通常用于提交到表单操作页面。这应该是“GET”。
我总是使用 Firebug 检查 success: 函数返回的数据格式,然后添加代码来显示它。由于上面的返回值发生了变化,它应该成为一个更简单的 json 对象来使用。
There's a lot of things that could use tweaking in the code.
I would add this line of code right before the .ajax() call:
@Jason Tabler's response is close. RecordCount of a query using COUNT() will always return a value of 1 (one). I would change the cfreturn statement to be this:
Pound signs are not needed in the cfreturn and this will return the value instead of a query object.
The ajax call should not be a "POST". "POST" is usually used to submit to a form action page. This should be a "GET".
I always use Firebug to check the data format returned for the success: function and then add code to display it. Since the return was changed above it should become a much simpler json object to use.
按如下方式更改成功函数。由于函数不仅返回整个查询,因此您将在 usercount 参数中获得查询对象,而不仅仅是计数。您还将收到文本字符串,您可能需要使用 eval() 函数将其转换为 JSON 对象。
编辑:
这是完整的功能代码。我只是在研究 success 函数问题,但附加您在 success 之外编写的 SPAN 也会引起问题。
Change success function as per below.. As function return whole query not only count so you will get query object in usercount argument not only count. Also you will receive as text string you may need to convert to JSON object using eval() function.
EDIT:
Here is full functional code. I was looking into success function issue only but appending SPAN you had wrote outside success was also causing issue.