displaying an ajax response with jquery

2019-05-16 15:49发布

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.

2条回答
Animai°情兽
2楼-- · 2019-05-16 16:16

use datatype as HTML if you are sending response as html

$.ajax({
      type:'GET',
      url:'Asset.cfc?method=Asset&returnformat=json',
      dataType: "html",  //<---- here
      data: {
          asset_id:     caseid,
        },
      success:function(data) {
        if(data) {   // DO SOMETHING     
            $('#picoutput').html(data);
        } else { // DO SOMETHING }
      }
    }
   });
查看更多
我只想做你的唯一
3楼-- · 2019-05-16 16:19

Use data.value

  success:function(data) {
    if(data) {   // DO SOMETHING     
        $('#picoutput').html(data[0].value);
    } else { // DO SOMETHING }
  }
查看更多
登录 后发表回答