I am trying to get a piece of data back using jQuery ajax via a POST response, but I cannot seem to display the data. Here is the code that I am using.
$(document).ready(function(){
$('#contentdiv').html(' ');
$("#idm").change(function(){
var formval = {idm:$(this).val()};
$.ajax({
type: "POST",
url: "request_processor.cfm",
dataType: "json",
data: formval,
success: function(response){
$('#contentdiv').fadeIn(2000).append(response.DESCRIPTION);}
});
});
});
Here is the JSON I am getting back from the CFC.
{"DESCRIPTION":"Global Alpha I Benchmark"}
Here is the code in request_response.cfm
<cfset oData = CreateObject("component","menudata")>
<cfset oData.setDataSource(dsn)>
<cfset theData = oData.getMenuData(FORM.idm)>
<cfset oJSON = createObject("component","cfjson")>
<cfset theResults = oJSON.encode(theData)>
<cfoutput>#theResults#</cfoutput>
What is the problem?
Thanks
Take a look at the response you get from your ajax request in the network tool in firebug or the chrome developer tools. Make sure you are only getting the JSON back and nothing else.
If you are getting junk back before or after the JSON, then you need to restrict the output using cfsetting to prevent output other than the content between the cfoutput tags.
You should try actually call a method on a CFC rather than making a call to cfm page and using the returnformat attributes on the function and/or the JSON functions built into ColdFusion.
You should also make sure that your #contentdiv element is valid HTML. Have you looked at the source of the page once the response has come back? Is anything actually appearing in the DOM element you’ve given an id of “contentdiv”? If it is, then you have a CSS/javascript problem.
You should make it visible in the CSS and only do the append call without the fade(). You need to make sure that the “contentdiv” is actually capable of being visible on your page. Once you’ve got the description displaying on the page, then reintroduce the fade(). However, the fade should be chained after the append and not before.
To be honest, this is just debugging 101. Its not a direct answer, but hopefully you’ll find your own answer.