游.程 2015-07-24 17:49 采纳率: 0%
浏览 26

cfc的AJAX返回变量

I'm trying to perform an SQL COUNT on my database using AJAX thru a cfc file, however, I have no clue how to get the return variable. The cfc file is as follows

<cffunction name="getSpeakerCount" access="remote" returntype="query">
    <cfargument name="AID" required="true" type="string">
        <cfquery name="getSpeaker" datasource="SpeakerCard">
            SELECT COUNT(AID)
            FROM tbl_SpeakerCard_Log
            WHERE AID = '#AID#'
        </cfquery>
    <cfreturn getSpeakerCount>
</cffunction>

My ajax is along the lines of this... I'm not sure how to properly call/use it.

for (var i = 0; i < X; i++)
{
    $.ajax('actions/AgendaList.cfc?wsdl', {method : 'getSpeakerCount', AID: AgID, dataType: 
"text", type: 'get', success: function(data)
            {
                SCount[i] = data;
            }
            }});

This always returns all the elements in SCount[i] as undefined. I'm not sure how to get the count return from the cfc... Any help is appreciated!

  • 写回答

2条回答 默认 最新

  • 7*4 2015-07-24 18:49
    关注

    Your function will fail no matter how you call it. You say you want to return a number but, as pointed out in the comment, you are returning an undefined variable. This will fix that:

    • In your cffunction tag, change the return type from query to numeric.
    • In the select clause of your query, use an sql alias to give yourself a column name.
    • Since your query will only have one field and one row, return that value.

    So this:

    <cffunction name="getSpeakerCount" access="remote" returntype="query">
    

    becomes this:

    <cffunction name="getSpeakerCount" access="remote" returntype="numeric">
    

    and this:

    SELECT COUNT(AID)
    

    becomes this:

    SELECT COUNT(AID) records
    

    and this:

    <cfreturn getSpeakerCount>
    

    becomes this:

    <cfreturn getSpeaker.records>
    

    The comment about using query paramters is also valid. Additionally, you should scope that variable.

    So this:

    WHERE AID = '#AID#
    

    should be this:

    WHERE AID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.aid#">
    

    Make sure you can run this using ColdFusion code before trying it with ajax.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么