Calling a CFC with multiple arguments using AJAX

2.2k views Asked by At

I am using AJAX to call a CFC and I am trying to send over two variables from a query that I ran. My ajax looks like this:

    $.ajax({
     type:"POST",
     url:"cfcs/get_equipment_details.cfc?method=getDetails",
     dataType: 'json',
     data: { equipment_name: "#equipment_name#", customer_name: "#customer_name#"},
    cache:false,
    success: function(response) {
        console.log("Customer Name" + customer_name);

        $.each(response.DATA, function(i, row){
            var id = row[0];
            var customer_name = row[1];
            var equipment_name = row[2];
            var date_last_completed = row[3];
            var maintenance_interval = row[4];
            var last_completed_by = row[5];
            var equipment_notes = row[6];
            var warranty_end_date = row[7];
            var service_Tag = row[8];
            var serial_number = row[9];
            var backup_notes = row[10];
            var TT = row[11];
            $("##customer_name#id#").val(customer_name);
            });
     }
});

Here is my CFC:

<cffunction name="getDetails" access="remote" output="false" returntype="query">
    <cfargument name="equipment_name" type="any" required="true">
    <cfargument name="customer_name" type="any" required="true">

    <!--- Get list of all equipment --->    
    <cfquery name="get_equipment" datasource="#datasource#">
    select *
    from equipment_maintenance
    where equipment_name= <cfqueryparam value="#ARGUMENTS.equipment_name#" cfsqltype="cf_sql_varchar">
    AND customer_name= <cfqueryparam value="#ARGUMENTS.customer_name#" cfsqltype="cf_sql_varchar">
    </cfquery>

    <cfreturn get_equipment>

But my CFC is throwing an error:

The parameter customer_name to function getDetails is required but was not passed in.

How can I successfully pass in those two argument and return my query results?

1

There are 1 answers

5
Keshav jha On BEST ANSWER

You are missing closing " for customer_name in your data attributes of Ajax function. Your Ajax function should look something like this

$.ajax({
 type:"POST",
 url:"cfcs/get_equipment_details.cfc?method=getDetails",
 data: { equipment_name: "#equipment_name#", customer_name: "#customer_name#"},
        cache:false,
        success: function(msg) {
         $("##customer_name#id#").val(msg); 
    }
 });

Now there is some issue with your cfc function. you are returning a variable that dose not exist and its same as your function name. If you do something like this it will work

<cffunction name="getDetails" access="remote" output="false" returnformat="JSON">
    <cfargument name="equipment_name" type="any" required="true">
    <cfargument name="customer_name" type="any" required="true">

    <!--- Get list of all equipment --->    
    <cfquery name="get_equipment" datasource="#datasource#">
    select *
    from equipment_maintenance
    where equipment_name= <cfqueryparam value="#ARGUMENTS.equipment_name#" cfsqltype="cf_sql_varchar">
    AND customer_name= <cfqueryparam value="#ARGUMENTS.customer_name#" cfsqltype="cf_sql_varchar">
    </cfquery>

    <cfreturn get_equipment.customer_Name>
</cffunction>

If you want entire query object to be return as you ajax response then parse JSON on success of ajax and process it further