displaying an ajax response with jquery
45,548
Solution 1
Use data.value
success:function(data) {
if(data) { // DO SOMETHING
$('#picoutput').html(data[0].value);
} else { // DO SOMETHING }
}
Solution 2
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 }
}
}
});
Author by
Chris Pilie
Updated on May 03, 2020Comments
-
Chris Pilie about 4 years
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.