jquery cfml ajax

发布于 2024-12-29 23:18:51 字数 1153 浏览 4 评论 0原文

这令人沮丧。我读过的任何内容都没有阐明这一点。而且事情不应该这么复杂。这是我的代码的当前状态。我一直在测试每一种组合,希望它有意义。我想要从 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 技术交流群。

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

发布评论

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

评论(4

蒲公英的约定 2025-01-05 23:18:51

您必须了解,默认情况下,ajax 本质上是异步的,因此您必须等到服务器响应才能使用仅在成功处理程序中可用的 usercount。通过将 async 设置为 false 进行同步调用,或者等待服务器响应。

将代码移到成功处理程序中

$(".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);
       $(this).append($("<span>" + usercount +"</span>"));
     },
     error: function (xhr, textStatus, errorThrown){ 
      // show error 
      //alert(errorThrown);
      console.log('errorThrown');
    } 
  }); 

  }, 
  function () {
    $(this).find("span:last").remove();
  }
);

我不会建议您进行同步调用,因为它会使整个页面等待,有时如果服务器需要时间响应,浏览器会完全挂起。

You have to understand that ajax is async in nature by default so you have to wait until server responds before using usercount which is only available inside success handler. Either make a synchronous call by setting async to false or wait until the server responds.

Move your code inside success handler

$(".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);
       $(this).append($("<span>" + usercount +"</span>"));
     },
     error: function (xhr, textStatus, errorThrown){ 
      // show error 
      //alert(errorThrown);
      console.log('errorThrown');
    } 
  }); 

  }, 
  function () {
    $(this).find("span:last").remove();
  }
);

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.

苄①跕圉湢 2025-01-05 23:18:51

你为什么不尝试一下
cfreturn qResults.recordCount?
您现在正在返回整个查询对象。

Why don't you try
cfreturn qResults.recordCount?
You are returning the entire query object right now.

天赋异禀 2025-01-05 23:18:51

有很多东西可以在代码中进行调整。

我会在 .ajax() 调用之前添加这行代码:

$.ajaxSetup({ timeout: 5000, cache: false }); // this is needed or ie will cache the ajax responses!

@Jason Tabler 的响应很接近。使用 COUNT() 的查询的 RecordCount 将始终返回值 1(一)。我会将 cfreturn 语句更改为:

<cfreturn qResults.usercount />

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:

$.ajaxSetup({ timeout: 5000, cache: false }); // this is needed or ie will cache the ajax responses!

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

<cfreturn qResults.usercount />

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.

蓝眼泪 2025-01-05 23:18:51

按如下方式更改成功函数。由于函数不仅返回整个查询,因此您将在 usercount 参数中获得查询对象,而不仅仅是计数。您还将收到文本字符串,您可能需要使用 eval() 函数将其转换为 JSON 对象。

编辑:
这是完整的功能代码。我只是在研究 success 函数问题,但附加您在 success 之外编写的 SPAN 也会引起问题。

    $(".alphabet").hover(function () {
              var _$this = $(this);
              var usercount = 0;
              $.ajax({ type: "POST", 
              url: "scribble.cfc?method=CountUsersByLetter&returnformat=json", 
              data: "nbw=" + $(this.target).val(), 
              datatype: "html",
              success: function(res){
                 usercount = eval("(" + res + ")").DATA[0][0];
                 _$this.append($("<span> (" + usercount +")</span>"));
              },
              error: function (xhr, textStatus, errorThrown){ 
               console.log('errorThrown');
                } 
            }); 
        }, 
          function () {
            $(this).find("span:last").remove();
          }
        );

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.

    $(".alphabet").hover(function () {
              var _$this = $(this);
              var usercount = 0;
              $.ajax({ type: "POST", 
              url: "scribble.cfc?method=CountUsersByLetter&returnformat=json", 
              data: "nbw=" + $(this.target).val(), 
              datatype: "html",
              success: function(res){
                 usercount = eval("(" + res + ")").DATA[0][0];
                 _$this.append($("<span> (" + usercount +")</span>"));
              },
              error: function (xhr, textStatus, errorThrown){ 
               console.log('errorThrown');
                } 
            }); 
        }, 
          function () {
            $(this).find("span:last").remove();
          }
        );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文