本文介绍了通过JSON传递的JavaScript数组的ColdFusion CFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个JavaScript来捕捉一个flexigrid检查所有的复选框,并尝试行ID的数组发送到CFC

i use this javascript to capture all checkboxes checked in a flexigrid and try to send this array of row ids to an 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参数是类型的数组没有。

when i try to run this function, my server answers that The NUMSEQCERTIDAO argument passed to the removeCertidaoCRC function is not of type array.

我跑出来的,​​其中我进入刚刚上延迟的项目选项。

I'm running out of options on a delayed project in which i entered just recently.

推荐答案

jQuery将做发送分隔状 numSeqCertidao [] ='VAL1',numSeqCertidao [] ='val2的'数组的一个好工作... 但ColdFusion的处理这种严重​​的,并没有考虑这一点,并重建它作为一个数组。

jQuery will do a good job of sending the array separated like numSeqCertidao[] = 'val1', numSeqCertidao[] = 'val2' ... but ColdFusion handles this badly and does not take this and rebuild it as an array.

您的JavaScript不是的发送JSON格式的数据,具体的数据类型的选择是为响应数据的格式。为了你的需求,我建议你送 numSeqCertidao 作为一个列表,让您的CFC方法的参数类型的字符串,离开它的其余部分按原样,应该很好地工作。

Your JavaScript is not sending data in JSON format, the dataType option is for the format of the response data. For your needs I would suggest you send numSeqCertidao as a list, give your CFC method's argument a type of string and leave the rest of it as is, should work fine.

稍作修改code:

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>

这篇关于通过JSON传递的JavaScript数组的ColdFusion CFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 23:41