微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐