通过Json将javascript数组传递给coldfusion CFC

Passing javascript array to coldfusion CFC via Json(通过Json将javascript数组传递给coldfusion CFC)

本文介绍了通过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 格式的数据,dataType 选项用于响应数据的格式.根据您的需要,我建议您将 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.

略微修改的代码:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:通过Json将javascript数组传递给coldfusion CFC

基础教程推荐