Soujanya
Soujanya

Reputation: 287

How to return ColdFusion result to ajax response?

I am working with ColdFusion and am new to the language. I want to perform a database operation with the given value and return the result via ajax response, when the onBlur event of a textbox is triggered. Please check my code below

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("#username").blur(function() {
            alert("This input field has lost its focus.");
            var userNameVal = $(this).val();
            if(userNameVal != "") {
                $.get("myajax.cfc?method=getuser&userName=userNameVal", function(res) {
                    $("#userid").val=res;
                }, "JSON");
            }
        });
    });
</script>

<input type='textbox' name='username' id='username' />
<input type='hidden' name='userid' id='userid'/> 

myajax.cfc:

<cfcomponent>
  <cffunction name="getuser" access="remote" returntype="Integer">
    <cfargument name="userName" type="string" required="yes">
    <cftry>
      <cfquery name="getUser" datasource="ajax_example">
        select USER_ID 
        from user u 
        where u.firstname=userName; 
        <cfqueryparam value = "#url.userName#" cfsqltype="cf_sql_varchar">
      </cfquery>
      <cfcatch>
        <cfoutput>
          #cfcatch.Detail#<br />
          #cfcatch.Message#<br />
          #cfcatch.tagcontext[1].line#:#cfcatch.tagcontext[1].template#
        </cfoutput>
      </cfcatch>
    </cftry>
    <cfreturn getUser.USER_ID/> 
  </cffunction>
</cfcomponent>

From my ajax.cfc response I want to return USER_ID to my ajax call. How can I do this?

Upvotes: 1

Views: 1579

Answers (2)

Pankaj
Pankaj

Reputation: 1741

You need to pass the actual value of the variable named userNameVal in the query string. Like this:

$.get("myajax.cfc?method=getuser&userName="+userNameVal, function(res) {
           $("#userid").val=res;
}, "JSON");

And as @Dan suggested, the way you are using cfqueryparam, is wrong. It should be done like this:

<cfquery name="getUser" datasource="ajax_example">
    select USER_ID 
    from user u 
    where u.firstname=<cfqueryparam value = "#arguments.userName#" cfsqltype="cf_sql_varchar">
  </cfquery>

Upvotes: 1

Dan Bracuk
Dan Bracuk

Reputation: 20794

You have this code:

where u.firstname=userName; 
<cfqueryparam value = "#url.userName#" cfsqltype="cf_sql_varchar">

This is not valid sql. The semi-colon terminates the command and ColdFusion will throw an error because the cfqueryparam tag comes afterwards.

You also have this query in a cftry block. The cfcatch code outputs data. If you were calling this function from ColdFusion code, you would see the cfcatch information in your web browser. However, you are calling it with ajax. It's outputting to a black hole. Also, you are getting no ColdFusion error because of the try/catch. Plus the cfcatch block does not include a cfreturn tag so nothing gets returned to javascript.

Another error is that you are using the wrong scope in your query. This:

value = "#url.userName#"

should be this:

value = "#arguments.userName#"

You are attempting to do too many things at once. I suggest calling your method with ColdFusion until you get it working properly. Then call it with ajax.

Upvotes: 1

Related Questions