displaying an ajax response with jquery

Chris Pilie picture Chris Pilie · Mar 15, 2013 · Viewed 42.1k times · Source

I have a Coldfusion cfc that queries a database for data. I would like to call that cfc and display the ajax response within a div. Eventually I would like to format the response with html. Currently I am having trouble with displaying the response. This is what I have so far.

Here is the cfc : Asset.cfc

<cffunction name="Asset" access="remote" returntype="array">
        <cfargument name="asset_id" type="string" required="yes">

         <!--- Define the local scope. --->
          <cfset var LOCAL = {} />    
          <cfset var qPics = "" />
          <cfset var result = arrayNew(1) />
          <cfset var PicStruct  = '' />

        <cfquery name="Pics">
        SELECT DISTINCT aq.ID
        FROM AAssignment a 
        INNER JOIN Assets aq ON aq.ID = a.Asset
        WHERE a.AssetItem = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.assetgrp_id#">                
        </cfquery>

         <cfloop query="Pics">
            <cfset PicStruct = StructNew() />
            <cfset PicStruct["value"] = ID />
            <cfset ArrayAppend(result,PicStruct) />
          </cfloop>

        <cfset myResult="#result#">
        <cfreturn myResult>
    </cffunction>

Here is the jquery

   <script>
    var caseid = <cfoutput>'#URL.ID#'</cfoutput>

    $.ajax({
          type:'GET',
          url:'Asset.cfc?method=Asset&returnformat=json',
          dataType: "json",
          data: {
              asset_id:     caseid,
            },
          success:function(data) {
            if(data) {   // DO SOMETHING     
                $('#picoutput').html(data);
            } else { // DO SOMETHING }
          }
        }
       });
    </script>

<div id="picoutput"></div>

Right now I get this response back from the cfc in Firebug.

[{"value":"3277C2B9-155D-D714-40143A59A8290252"}]

However it will not display in the div.

Answer

Vitthal picture Vitthal · Mar 15, 2013

Use data.value

  success:function(data) {
    if(data) {   // DO SOMETHING     
        $('#picoutput').html(data[0].value);
    } else { // DO SOMETHING }
  }