Passing an array with jquery to the ColdFusion component

Before I get started, I'm admittedly very new to jquery, in fact I haven't had a javascript background since 3 months ago.

I am trying to execute an array of information available in jquery and pass that array to a .cfc file where the information can be processed.

Here is my jquery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.json.min.js"></script>
<script type="text/javascript">
  $(function() {
    var mydata = {data:[1,2,3,4,5]};
																						
    $.post('test.cfc', {method:"handleArray", returnFormat:"plain", argumentCollection:   $.toJSON(mydata)}, function(res) {
    alert($.trim(res));
    });
  });
</script>
Run codeHide result

This was a code snippet found from a post by Stephen Duncan Jr. about 5 years ago.

Here's the cold fusion:

<cfcomponent>

<cffunction name="handleArray" access="remote" returnType="numeric">
<cfargument name="data" type="array" required="true">

  <cfquery name = "qTest" datasource="#REQUEST.dsn#" username="#REQUEST.dsu#" password="#REQUEST.dsp#"> 
    INSERT INTO test
      (value1, value2, value3, value4, value5)
    VALUES (
      <cfqueryparam cfsqltype="cf_sql_integer" value="#handleArray[1]#">,
      <cfqueryparam cfsqltype="cf_sql_integer" value="#handleArray[2]#">,
      <cfqueryparam cfsqltype="cf_sql_integer" value="#handleArray[3]#">,
      <cfqueryparam cfsqltype="cf_sql_integer" value="#handleArray[4]#">,
      <cfqueryparam cfsqltype="cf_sql_integer" value="#handleArray[5]#">)
    </cfquery>

<cfreturn arrayLen(arguments.data)>
</cffunction>

</cfcomponent>
Run codeHide result

This code, of course, is just a test to make it work. But I need to pass about 40 variables to cfc.

Any information that can be provided will be highly appreciated, especially anything that explains why the result works.

+4
1

@TimHowey, , jquery post. , :

<script type="text/javascript" src="jquery.json.min.js"></script>
<script type="text/javascript">
  $(function() {
      var mydata = "[1,2,3]";
      $.post('test.cfc', {
          method: "handleArray",
          returnFormat: "plain",
          data: mydata
      }, function(res) {
          alert("ok");
      });
  });
</script>

, form. cfc , , - :

    <cffunction name="handleArray" access="remote" returnType="numeric">
        <cfargument name="data" type="any" required="true">
        <cfset var getArray = DeserializeJSON(arguments.data)/>
        <cfset var qTest = "">

        <cfquery name = "qTest" datasource="#REQUEST.dsn#" username="#REQUEST.dsu#" password="#REQUEST.dsp#"> 
            INSERT INTO test
            (value1, value2, value3)
            VALUES (
                <cfqueryparam cfsqltype="cf_sql_integer" value="#getArray[1]#">,
                <cfqueryparam cfsqltype="cf_sql_integer" value="#getArray[2]#">,
                <cfqueryparam cfsqltype="cf_sql_integer" value="#getArray[3]#">
          )
        </cfquery>

        <cfreturn arrayLen(getArray)>
    </cffunction>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.json.min.js"></script>
<script type="text/javascript">
  $(function() {
      var mydata = '[1,"keshav",3]';
      $.post('test.cfc', {
          method: "handleArray",
          returnFormat: "plain",
          data: mydata
      }, function(res) {
          alert("ok");
      });
  });
</script>
+2

Source: https://habr.com/ru/post/1613695/


All Articles