Skip to main content
Inspiring
May 22, 2014
Answered

cffunction array parameter

  • May 22, 2014
  • 2 replies
  • 1535 views

I have following code to get an array from jQuery to pass MS SQL Server stored procedure to update my tables.

Is it the right way to access array from passing jQuery function?

Your help and information is great appreciated,

Regards,

Iccsi,

<cffunction name="MyFunction" access="remote">

  <cfargument name="MyData" type="any" required="true">

<cfif isJSON(arguments.MyData)>

        <cfset arguments.MyData = deserializeJSON(arguments.MyData)>

    </cfif>

    <cfstoredproc procedure = "MySP">

   <cfprocparam value = "#arguments.MyData[1]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[2]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[3]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[4]#" CFSQLTYPE = "cf_sql_varchar">

   <cfprocparam value = "#arguments.MyData[5]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[6]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[7]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[8]#" CFSQLTYPE = "CF_SQL_LONGVARCHAR">

   <cfprocparam value = "#arguments.MyData[9]#" CFSQLTYPE = "CF_SQL_LONGVARCHAR">

   <cfprocparam value = "#arguments.MyData[10]#" CFSQLTYPE = "cf_sql_date">

   <cfprocparam value = "#arguments.MyData[11]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[12]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[13]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[14]#" CFSQLTYPE = "cf_sql_varchar">

   <cfprocparam value = "#arguments.MyData[15]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[16]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[17]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[18]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[19]#" CFSQLTYPE = "cf_sql_numeric">

   <cfprocparam value = "#arguments.MyData[20]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[21]#" CFSQLTYPE = "cf_sql_date">

   <cfprocparam value = "#arguments.MyData[22]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[23]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#arguments.MyData[24]#" CFSQLTYPE = "cf_sql_integer">

   

</cfstoredproc>

</cffunction>

This topic has been closed for replies.
Correct answer BKBK

Further remarks.

(1) The suggestion "MyServer.cfc?method=MyFunction" implies type: "GET".

(2) The JQuery line

data: { argumentCollection: JSON.stringify([$("#txtField1").val(), etc])} implies

<cffunction name="MyFunction" access="remote">

<cfargument name="argumentCollection" type="any" required="true">

</cffunction>

2 replies

BKBK
Community Expert
Community Expert
May 23, 2014

One more remark, in addition to what Carl has said. The code assumes an array, so you must test for its existence beforehand.

<cffunction name="MyFunction" access="remote">

<cfargument name="MyData" type="any" required="true">

<cfset var thisData = arguments.MyData>

<cfif isJSON(arguments.MyData)>

        <cfset thisData = deserializeJSON(arguments.MyData)>

</cfif>

<cfif isArray(thisData)>

<cfstoredproc procedure = "MySP">

   <cfprocparam value = "#thisData[1]#" CFSQLTYPE = "cf_sql_integer">

   <cfprocparam value = "#thisData[2]#" CFSQLTYPE = "cf_sql_integer">

<!--- etc.--->

<cfelse>

<!--- What to do if there is no array--->

</cfif>

iccsiAuthor
Inspiring
May 23, 2014

Thanks for the information and help,

I got WDDX packet parse error at line 1 column 1 content is not allowed in prolog...

  when I send the array to the cffunction.

I use CFoutput, but it does not show the data I sent.

It seems that CF does not recognize the array sent from my client from jQuery.

Thanks again for helping and information,

Regards,

Iccsi,

Carl Von Stetten
Legend
May 23, 2014

It sounds like ColdFusion doesn't realize that the data being sent is JSON, and is assuming it is WDDX-wrapped XML. 

  1. Can you tell which line of your code is causing the error (the WDDX packet parse error line number is probably the line of the incoming data that causes the error, not the line in your code)? 
  2. Can you show the jQuery code that submits the data to ColdFusion (as you might be missing an argument that sets the content type)?
  3. What does the data in your AJAX request look like? (Use your browser's developer tools or a sniffer like Fiddler or Charles to examine the request content)

-Carl V.

Carl Von Stetten
Legend
May 22, 2014

I could very well be wrong, but I don't believe it's recommended to replace values of variables in the arguments scope.  Instead of

<cfset arguments.MyData = deserializeJSON(arguments.MyData)>

I would maybe do

<cfset local.MyData = deserializeJSON(arguments.MyData)>

Then use local.MyData instead of arguments.MyData in all of your subsequent code.  That way the original argument is preserved (in case you find the need to use it down the road).

I'm also a bit uncomfortable assuming that the array coming from your AJAX request is valid and all values are in the correct order.  It very well might be that your front-end code assembles that array perfectly, but I wouldn't count on that always being the case.  You need to do some validation to make sure that each value in the array is present and not null (or empty), and is the correct type.  You might consider submitting a JSON object (key/value pairs) instead of an array, as that will be deserialized into a ColdFusion struct.  You could then refer to each value in the struct by keyname instead of array position (so the order of values in the JSON object would not matter), and validating each key/value pair might be easier than validating the array.

-Carl V.