调用Coldfusion组件函数的jQuery post方法不起作用

时间:2015-11-13 16:49:31

标签: javascript jquery ajax coldfusion

我试图通过使用jQuery的post()方法通过ajax调用cfc方法,但我一直收到一条错误消息,说“yyy函数的xxx参数是必需的,但未传入”。这是我的cfc功能:

static char result[1000];

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
    if (fdwReason == DLL_PROCESS_ATTACH)
    {
        char* cid0 = GetCpuID(0);
        char* cid1 = GetCpuID(1);
        StringCchPrintfA(buffer, 1000, "%s%s", cid0, cid1);
    }
    return TRUE;
};

extern "C" TESTDLL_API char* SelectColor()
{
    return result;
}

这是调用它的jQuery代码:

<cffunction name="updateAssessment" returntype="struct" returnformat="json" access="remote" hint="For ajax update of a single assessment record">
    <cfargument name="data" type="JSON" required="true" hint="Data structure received from ajax call">

    <cfset var incomingData = "" />
    <cfset var qry = "" />
    <cfset var resultset = {success:false,message:"invalid data"}>

    <cfif IsJSON(arguments.data)>
        <cfset incomingData = deserializeJSON(arguments.data) />
    </cfif>

    <cfreturn resultset />
</cffunction>

我正在使用“data:JSONPacket”将所需参数传递给cfc函数,所以不确定问题是什么。

2 个答案:

答案 0 :(得分:1)

我认为您需要单独传递参数。现在的方式是,jQuery将data结构作为字符串传递给data[source]

这样的键
$('.ajaxSubmitBtn').on('click', function() {

    //grab values from editable table cells...
    var assessmentid =  $.trim($(this).closest('tr').find('.ajaxSubmitBtn').val());
    var source =  $.trim($(this).closest('tr').find('.sourceTD').text());
    var lname =   $.trim($(this).closest('tr').find('.lnameTD').text());
    var fname =   $.trim($(this).closest('tr').find('.fnameTD').text());
    var ssn =   $.trim($(this).closest('tr').find('.ssnTD').text());
    var assessdate = $.trim($(this).closest('tr').find('.assessdateTD').text());
    var casehranum = $.trim($(this).closest('tr').find('.casehranumTD').text());
    var dob = $.trim($(this).closest('tr').find('.dobTD').text());

    //Put values in a JSON object...
    var JSONPacket =  {
        "method" : "updateAssessment",
        "assessmentid":assessmentid,
        "source":source,
        "lname":lname,
        "fname":fname,
        "ssn":ssn,
        "assessdate":assessdate,
        "casehranum":casehranum,
        "dob":dob
        }

    //call cfc function via ajax using post()
    $.post("../../../cfc/starsImporter.cfc", JSONPacket,
        function(response) {
            console.log(response);
        },
    "json");
 });

<强> CFC

<cffunction name="updateAssessment" returntype="struct" returnformat="json" access="remote" hint="For ajax update of a single assessment record">
    <cfargument name="assessmentid" type="string" required="true" />
    <cfargument name="source" type="string" required="true" />
    <cfargument name="lname" type="string" required="true" />
    <cfargument name="fname" type="string" required="true" />
    <cfargument name="ssn" type="string" required="true" />
    <cfargument name="assessdate" type="string" required="true" />
    <cfargument name="casehranum" type="string" required="true" />
    <cfargument name="dob" type="string" required="true" />
    ...

答案 1 :(得分:1)

知道了 - 问题是我需要在将JSONPacket发布到cfc方法之前对其进行字符串化。并且,在创建JSON对象后添加分号。所以一起看起来像这样:

<cffunction name="updateAssessment" returntype="struct" returnformat="JSON" access="remote" output="false" hint="For ajax update of a single assessment record">
    <cfargument name="JSONPacket" type="string" required="true" hint="Data structure received from ajax call">

    <cfset var incomingData = "" />
    <cfset var qry = "" />
    <cfset var resultset = {success:false,message:"invalid data"}>

    <cfif IsJSON(arguments.JSONPacket)>
        <cfset incomingData = deserializeJSON(arguments.JSONPacket) />
    </cfif>

    <cfreturn resultset />
</cffunction>


 $('.ajaxSubmitBtn').on('click', function() {
        //console.log("Hello");
        var assessmentid =  $.trim($(this).closest('tr').find('.ajaxSubmitBtn').val());
        var source =  $.trim($(this).closest('tr').find('.sourceTD').text());
        var lname =   $.trim($(this).closest('tr').find('.lnameTD').text());
        var fname =   $.trim($(this).closest('tr').find('.fnameTD').text());
        var ssn =   $.trim($(this).closest('tr').find('.ssnTD').text());
        var assessdate = $.trim($(this).closest('tr').find('.assessdateTD').text());
        var casehranum = $.trim($(this).closest('tr').find('.casehranumTD').text());
        var dob = $.trim($(this).closest('tr').find('.dobTD').text());
        var JSONPacket =  {
            "assessmentid":assessmentid,
            "source":source,
            "lname":lname,
            "fname":fname,
            "ssn":ssn,
            "assessdate":assessdate,
            "casehranum":casehranum,
            "dob":dob
            };

        $.post("../../../cfc/starsImporter.cfc", {method:"updateAssessment", JSONPacket:JSON.stringify(JSONPacket)},
            function(response) {
                console.log(response);
            },
        "JSON");
     });