Skip to main content
Participant
July 20, 2009
Question

cfajaxproxy / cfc / javascript issue (returned number being altered)

  • July 20, 2009
  • 1 reply
  • 650 views

Ok, guys, this is a wierd one. I am using CF8 with IIS, and am using the cfajaxproxy method to call a CFC using javascript.  In this particular case, it is a very simple CFC with one method used to determine if a string passed (an email address in this case) exists in the database.  The value returned to the JS function is just the recordcount of very a simple query in the CFC.

The problem is this:  In the JS function handling the the returned result, the returned result does not match what is being passed from the CFC!

Specifically, if you alert of otherwise display the result in the JS, it somehow has ".02009955946" appended to it.  (So if the CFC recordcount is 0, the JS sees "0.02009955946", if the CFC recordcount is 1, the JS sees "1.02009955946" etc.)  I have added code to the CFC to report the returned value independently (via cfmail), and it is being returned from the CFC correctly. But alerting the value returned to the JS function results in the odd results mentioned above.

Here is the actual code:

From the CFC:

<cfcomponent displayname="userEmailCheck">
   
    <cffunction name="userEmailDupeCheck" access="remote" returntype="numeric" hint="This function determines if a submitted user email address already exists in the DB.">
       
        <cfargument name="emailAddress" type="String" required="false">
       
        <cfset var numDupeEmail = 0>
       
        <cfif arguments.emailAddress NEQ "">

            <cfquery name="checkEmailinDB" datasource="#request.dsn#">
                select wcs_user_id
                from wcs_user_tb
                where wcs_login = '#arguments.emailAddress#'
            </cfquery>   
   
            <cfset numDupeEmail = checkEmailinDB.recordcount>
   
        </cfif>

        <cfreturn numDupeEmail>
   
    </cffunction>   

</cfcomponent>

And here is the relevant JS code from the CFM page:

...

<cfajaxproxy cfc="cfcs.userEmailCheck" jsclassname="userEmailCheckProxy">


<script>


function emailCheckSuccess(theResult)
            {

            alert(document.getElementById('email').value);
             alert(theResult);


            if (document.getElementById('email').value != '' && theResult > 0)
                {

                alert('This email address already exists in the database.\n\nPlease enter an unique address if you wish to add a new user.');
                document.getElementById('email').select();
                }
            }  


function theEmailDupeCheck(emailAddress)
            {
            var instance = new userEmailCheckProxy();
            instance.setCallbackHandler(emailCheckSuccess);
            instance.userEmailDupeCheck(emailAddress);
            }


</script>

...

[inside the cfform on the page...]

<cfinput name="email" id="email" type="Text" size="35" value="" defaultValue="" required="Yes" message="Please enter Email address" onBlur="theEmailDupeCheck(this.value);">

Has anyone seen this before?  I can manipulate the returned JS value to trim the extraneous garbage, but I am concerned that it shouldn't be there in the first place.

Any ideas as to why this happening???

Thanks in advance,

David

    This topic has been closed for replies.

    1 reply

    David_LAuthor
    Participant
    July 20, 2009

    Oh, and as a further test, i created a seperate page that invoked the CFC / function directly and it returned the value correctly:

    <cfinvoke component="cfcs.userEmailCheck"
        method="userEmailDupeCheck"
        returnvariable="NumEmailsFound">
       
    <cfinvokeargument
        name="emailAddress"
        value="foo@foo.com"
       >

       
    </cfinvoke>

    <cfoutput>
    NumEmailsFound: #NumEmailsFound#
    </cfoutput>

    I.E. NumEmailsFound is displayed as the correct recordcount from the query in the CFC...no extra junk.

    ilssac
    Inspiring
    July 20, 2009

    It could just be a small miss translation since for systems that only understand two numbers, 0 and 1.  Computers can have trouble with any other value.

    But, just in case, you have an un-'VAR'ed variable in your function and that can lead to strange behaviour.  I would fix that, even if it has no effect on the number being returned.

    <cfquery name="checkEmailinDB" datasource="#request.dsn#">

    The record set name used in your query tag is not var scoped, so it is not isloated to this function.  I would add a var definition for it at the top of the function.

    <cfset var checkEmailinDB = "">

    And while we are tweaking your code, there is really no need for the extra variable to run a value.  Instead of asigning the record count of the checkEmailinDB to another variable to return, just return checkEmailinDB.recordCount directly.

    I.E.

    <cfreturn checkEmailinDB.recordcount>

    These fixes only have a small chance of fixing the extra digits in the return value, but they are good to fix anyway.