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

我使用此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参数不是数组类型.

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

解决方法:

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>

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...