使用AJAX

时间:2016-12-22 20:52:30

标签: ajax coldfusion cfc

我正在使用AJAX来调用CFC,我正在尝试从我运行的查询中发送两个变量。我的ajax看起来像这样:

    $.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);
            });
     }
});

这是我的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>

但是我的CFC犯了一个错误:

  

参数customer_name to function getDetails是必需的,但是   没有通过。

如何成功传递这两个参数并返回我的查询结果?

1 个答案:

答案 0 :(得分:4)

您在Ajax函数的数据属性中缺少"的结束customer_name。 你的Ajax函数看起来应该是这样的

$.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); 
    }
 });

现在您的cfc功能存在一些问题。您将返回一个不存在的变量,它与您的函数名称相同。如果你做这样的事情就行了

<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>

如果你希望在ajax响应时返回整个查询对象,那么在ajax成功时解析JSON并进一步处理它