小编典典

通过Json将JavaScript数组传递给Coldfusion CFC

ajax

我使用此javascript捕获flexigrid中选中的所有复选框,然后尝试将此行ID数组发送到CFC

function removeCertidao(){
    var allVals = [];
    $("input[id='certidao']:checked").each(function() {
        allVals.push($(this).val());
    });
    if (allVals.length == 0) {
        alert('É necessário escolher ao menos uma certidão.');
        return false;
    } else {
        alert(allVals);
    }
    $.ajax({
        type: "post",
        url: "../../CFC/CRC.cfc",
        data: {
            method: "removeCertidaoCRC",
            numSeqCertidao: allVals,
        },
        dataType: "json",
        success: function(){
            alert('YES');
        },
        error: function(){
            alert('NO');
        }
    });             
 }

低于CFC

<cffunction access="remote" name="removeCertidaoCRC" returntype="boolean">
    <cfargument name="numSeqCertidao" type="array" required="true">
<cftry>
    <cftransaction>
        <cfquery datasource="portalCompras">
        UPDATE CRC_CERTIDAO CC
           SET CC.ncdcrcstatus = 0
             WHERE CC.NCDCRCCERTIDAO in <cfqueryparam value="#numSeqCertidao#"
                                                          cfsqltype="cf_sql_integer"
                                                          list="yes">
    </cfquery>
        </cftransaction>
    <cftransaction action="commit" />
    <cfreturn 0>
    <cfcatch type="any">
        <cftransaction action="rollback" />
        <cfreturn #cfcatch.message#>
        </cfcatch>
</cftry>
</cffunction>

当我尝试运行此函数时,我的服务器回答说,传递给removeCertidaoCRC函数的NUMSEQCERTIDAO参数不是数组类型。

我最近刚进入的一个延迟项目中的所有选项都用光了。


阅读 235

收藏
2020-07-26

共1个答案

小编典典

jQuery可以很好地将数组分开发送,numSeqCertidao[] = 'val1', numSeqCertidao[] = 'val2' ...但是ColdFusion不能很好地处理此问题,因此不接受并将其重建为数组。

您的JavaScript并非以JSON格式 发送
数据,dataType选项用于响应数据的格式。出于您的需要,我建议您numSeqCertidao以列表形式发送,为CFC方法的参数提供一个字符串类型,其余部分保持原样,应该可以正常工作。

稍作修改的代码:

function removeCertidao(){
    var allVals = [];
    $("input[id='certidao']:checked").each(function() {
        allVals.push($(this).val());
    });
    if (allVals.length == 0) {
        alert('É necessário escolher ao menos uma certidão.');
        return false;
    } else {
        alert(allVals);
    }
    $.ajax({
        type: "post",
        url: "../../CFC/CRC.cfc",
        data: {
            method: "removeCertidaoCRC",
            numSeqCertidao: allVals.join(),
        },
        dataType: "json",
        success: function(){
            alert('YES');
        },
        error: function(){
            alert('NO');
        }
    });             
 }

<cffunction access="remote" name="removeCertidaoCRC" returntype="boolean">
    <cfargument name="numSeqCertidao" type="string" required="true">
<cftry>
    <cftransaction>
        <cfquery datasource="portalCompras">
        UPDATE CRC_CERTIDAO CC
           SET CC.ncdcrcstatus = 0
             WHERE CC.NCDCRCCERTIDAO in <cfqueryparam value="#numSeqCertidao#"
                                                          cfsqltype="cf_sql_integer"
                                                          list="yes">
    </cfquery>
        </cftransaction>
    <cftransaction action="commit" />
    <cfreturn 0>
    <cfcatch type="any">
        <cftransaction action="rollback" />
        <cfreturn #cfcatch.message#>
        </cfcatch>
</cftry>
</cffunction>
2020-07-26