Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

displaying an ajax response with jquery

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.

like image 879
Chris Pilie Avatar asked Mar 15 '13 17:03

Chris Pilie


1 Answers

Use data.value

  success:function(data) {
    if(data) {   // DO SOMETHING     
        $('#picoutput').html(data[0].value);
    } else { // DO SOMETHING }
  }
like image 66
Vitthal Avatar answered Oct 13 '22 03:10

Vitthal