简体   繁体   中英

AJAX return variable from cfc

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!

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.

(Edit: Took too long typing and Dan beat me to the punch on most of it ;-) Leaving it here as more of an extended comment. See the additional points on testing, return types and formats below).

As mentioned in your other thread , it seems like you are trying to tackle too much at once. Approach it one piece at a time. Before you even think about calling a remote function from javascript, verify it works as expected when invoked from a browser.

There are several issues with the function.

  • You say you want to return a simple "count", but the function is returning the entire query object. If you wish to return a simple number, return the count(AID) value instead.

  • In order to use the count(AID) outside the query, you need to use a column alias in your SQL ie SELECT count(AID) AS YourAliasHere .... .

  • The function returns a nonexistent variable, ie <cfreturn getSpeakerCount> . To return the count, use the query column instead: <cfreturn getSpeaker.yourAliasHere> . Be sure to modify the function returnType as well. It should match the value returned by the function, in this case "numeric".

  • By default, functions use returnformat=wddx . If you want a different format you need to specify it in the URL, or function signature.

  • Lastly, test the function in a browser to ensure that it is returning the expected value - a simple number. Be sure to do a "view source" as well, as wddx will not be fully visible on screen due to the < and > .

    http://yourserver.com/path/to/AgendaList.cfc?method=getSpeakerCount&AID={someTe‌​stValueHere}&returnformat={desiredFormatHere}

As an aside, though it will not cause an error, be sure to localize all function local variables: that includes query names . Assuming you are running CF9+, use the Local scope: <cfquery name="Local.getSpeaker"...>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM