Reputation: 1257
I have been working on a problem in my ColdFusion application whereas I'm tring to populate the contents of a DIV based on the selection from a CFSelect box. After I make a selection, I have an AJAX call that executes a query in a CFC that then is supposed to return some data to display in my DIV.
Here is a link to my previous question
I received some great help to get my basic code correct, but I am having a sepearate issue now. My AJAX call is not returning any data and the "Error:" part of my AJAX function is firing indicating an issue with my request. Firebug doesn't show me anything helpful.
Can anyone help?
My SelectBox
<cfselect queryPosition="below" name="company_name" id="company_name" value="company_name" bind="cfc:cfcs.taxdata.getData()" bindonload="true" tabindex="0" >
<option>---Make A Selection---</option>
</cfselect>
My Javascript
<script>
// populate the dropdown
// on change of dropdown value return the cfc data into the div
$("##company_name").change(function() {
var selectValue = $("##company_name").val();
$.ajax({
type: "GET",
url:"cfcs/alertData.cfc?method=getAlerts",
dataType: "json",
data: {
company_name: selectValue
},
success: function (data) {
$("##test").html(data);
},
error: function (data) {
$("##test").html('Failed');
}
});
});
</script>
My Div
<div id="test">test</div>
My CFC
<cffunction name="getAlerts" access="remote" returntype="query">
<cfargument name="company_name" type="any" required="true">
<!--- localize function variables --->
<cfset var alertDetail = "">
<cfquery name="getID" datasource="#datasource#" >
select customer_id
from customer_table
where company_name = <cfqueryparam value="#ARGUMENTS.company_name#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfquery name="alertDetail" datasource="#datasource#">
SELECT ID
FROM customer_alerts
<!--- adjust cfsqltype if needed --->
WHERE customer_id = <cfqueryparam value="#getID.customer_id#" cfsqltype="cf_sql_varchar"> AND alert_status = 'on'
</cfquery>
<cfreturn alertDetail>
</cffunction>
Upvotes: 2
Views: 239
Reputation: 106
First, remember to add "returnFormat: 'json'" to your ajax request. This will automatically convert CF objects to a json format. By default ColdFusion will return in WDDX format (an XML spec). Next, understand that ColdFusion queries, serialized to JSON, are returned in an odd format. You might look at my serializeCFJSON jquery plugin (available on GitHub). Last, ditch CFSELECT. The CFFORM element types are extremely basic and inflexible.
Here's a post on how I do ajax with ColdFusion:
http://www.cutterscrossing.com/index.cfm/2011/9/26/How-I-Do-Things-ColdFusion-and-Ajax-Requests
And a link to my plugin:
https://github.com/cutterbl/serializeCFJSON
Upvotes: 2