0
votes

This is the jquery code in ColdFusion index.cfm which is calling chkUsername of cfc file.

<script type="text/javascript">
 chkUsernameUnique = function(theUsername){
  $.getJSON("selectuser.cfc", {
    method: 'chkUsername',
    Username: theUsername,
    returnformat: 'json'
}, function(isUsernameUnique){

    if (isUsernameUnique == true) {
        $("#theErrorDivID").html('Username is unique');
    }
    else {
        $("#theErrorDivID").html('Please select a new username');
    }
});
};
</script>

This is my component which is getting called from the cfm file.

<cfcomponent>
<cffunction name="chkUsername" returnformat="json" output="false">
    <cfargument name="username" required="true">

    <cfquery name="chkUsername" datasource="myDataSource">
    SELECT ID FROM employees_login WHERE username = <cfqueryparam value="#arguments.username#" cfsqltype="cf_sql_varchar" />
    </cfquery>

    <cfreturn yesNoFormat(chkUsername.recordCount) />
</cffunction>
</cfcomponent>

Nothing was getting displayed. I tried to check the error on fire bug and i am getting this error -

"NetworkError: 500 Internal Server Error:http://localhost:8500/workspace/UniqueUsername/selectuser.cfc?method=chkUsername&Username=admin&returnformat=json"

2
You should be getting more than that back. In Firebug, seleect Console > HTML and you should be able to see the full error returned. - Jason
Also I just noticed no space between name="chkUsername"returnformat="json" in your function declration.. may be causing your error ? - Jason
Is the path to the selectuser.cfc file correct? It's looking for it in the root of your project relative to the calling page (so in the same directory as the index.cfm page). If you try to visit localhost:8500/workspace/UniqueUsername/selectuser.cfc in your browser, are you presented with the ColdFusion Administrator login screen (which would be the case if the .cfc was at that address) or with a 500 error? - Matt Gifford
I got the root cause for it. I have written access="remote" in my function chkUsername. and now its working fine - Sonam Daultani
Yes path to cfc file is correct. There was some other problem with the function. Thanks - Sonam Daultani

2 Answers

2
votes

I was working on it from long time. Finally i got the answer. You need to write access="remote" in the cffunction tag like this-

<cfcomponent>
 <cffunction name="chkUsername" returnformat="json" access="remote" output="false">
  <cfargument name="username" required="true">

  <cfquery name="chkUsername" datasource="myDataSource">
   SELECT ID FROM employees_login WHERE username = <cfqueryparam value="#arguments.username#" cfsqltype="cf_sql_varchar" />
  </cfquery>

  <cfreturn yesNoFormat(chkUsername.recordCount) />
  </cffunction>
</cfcomponent>
0
votes

Try replacing this: <cffunction name="chkUsername"returnformat="json" output="false">

with this: <cffunction name="chkUsername" returnformat="json" output="false">