Search code examples
jqueryajaxcfc

How do I get multiple values from ajax cfc? I keep getting obj obj in my text field


I'm kind of stuck, I am making an Ajax call from a cfm to a .cfc. I am trying to retrieve two results, a "label" and a "value". This is what my JSON returned looks like:

[{"value":"H55","label":"JOHN SMITH"}]

However, when I try to have my results come into my empty form field, it just gets filled with [object Object]

Any help would be greatly appreciated!

$('#ckPin').click(function() { 
var RqPin = $("#RqPin").val(); 
    $.ajax({
    url: "autocomplete.cfc?method=getRqstPin", 
    type: "post",
    dataType: "json",
    data: { RqPin: ( RqPin ) }, 
    success:  function(data) {
            //alert(data);
              alert(JSON.stringify(data));
            //$("#rqFOC").val(data["FOC"]); 
              $("#rqName").val(data[0]);
        }, //close of SUCCESS
    error: function (xhr, textStatus, errorThrown){
              alert(errorThrown); 
        } //close of ERROR
    }); //close of AJAX
});//close of CLICK

and here is my .cfc

<cffunction name="getRqstPin" access="remote" returntype="any" returnformat="json"> 
    <cfargument name="RqPin" default="">
    <cfquery name="getRqstPin" datasource="RADIUS">
                 SELECT [Fname] + '' + [Lname] as Fullname
                       ,[FOC]
                  FROM [pins].[dbo].[Pinsdata]
                  Where PIN = #arguments.RqPin#
    </cfquery>

   <cfset returnArray = arrayNew(1)>
    <cfloop query="getRqstPin">
        <cfset resultStruct = StructNew() />
        <cfset resultStruct["label"] = Fullname />
        <cfset resultStruct["value"] = FOC />
        <cfset ArrayAppend(returnArray,resultStruct) />
    </cfloop>
    <cfreturn returnArray>
</cffunction>

Solution

  • data[0] is an object.Either you you should set the value or label to the val() to $("#rqName").

     $("#rqName").val(data[0].value);
    

    or

     $("#rqName").val(data[0].label);