4

Before I start, I am admittedly very new to jquery, in fact I had no javascript background as of 3 months ago.

What I'm trying to accomplish is taking an array of information that I have available in jquery, and passing 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>

This was a piece of code a 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>

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

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

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
Tim Howey
  • 145
  • 8
  • 4
    What is the specific problem or question? – charlietfl Oct 29 '15 at 17:35
  • 1
    The code doesn't work, and I don't know why. The alert in the jquery never fires, and I have to assume that's why the CFC isn't being run. – Tim Howey Oct 29 '15 at 18:02
  • What have you tried? What steps have you already taken to troubleshoot this issue? – Scott Stroz Oct 29 '15 at 18:14
  • 1
    Simple things first. I placed an alert before the var was created, after it's created, within the .post function and after the function. Everything fires except for the one inside the post function. I've also used cfabort tags within the cfc file, but it never makes it to any of them. Right now, the problem is 100% in the .post line, but I don't know what's wrong with it. – Tim Howey Oct 29 '15 at 18:18
  • Use a proper debugging tool like Firebug, etcetera. It makes debugging ajax a *lot* easier. You can see if requests are fired, chjeck the response, etcetera. *very new to jquery* First off did you verify the component works in CF first? I ask because a common mistake is to plug in jQuery before ensuring the server side code actually works, which makes problems even harder to debug... – Leigh Oct 29 '15 at 18:20
  • Here is where I originally got the code from, and from what the commentors have said, it's worked for them. http://www.raymondcamden.com/2010/11/01/Using-argumentCollection-with-AJAX-calls-to-ColdFusion-Components – Tim Howey Oct 29 '15 at 18:21
  • @Leigh I am using firebug as well. The $.post is never firing. As for the CFC, I started off using a cfinvoke tag and passing it an argument with no troubles. – Tim Howey Oct 29 '15 at 18:23
  • Suggested reading when asking for help: http://www.catb.org/esr/faqs/smart-questions.html http://sscce.org/ – Adam Cameron Oct 29 '15 at 19:29
  • 2
    Forgetting the AJAX side of things, what if you just call the CFC method directly in the browser address bar? – Adam Cameron Oct 29 '15 at 19:30

1 Answers1

2

@TimHowey there seems problem with your jquery post method. I think here is what you need to do:

<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>

You are using a post method so every thing will be sent in the form scope. In your cfc, all you need to do is to deserialize the argument like this:

    <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>

EDIT

<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>
Leigh
  • 28,765
  • 10
  • 55
  • 103
Keshav jha
  • 1,356
  • 8
  • 22
  • Ok, this is the first bit of light I've seen on my problem so far. I can see the information dumped in firebug, but it's not in the format I expected it would be. So now I'm confused how I get it from there into cfquery, but at least I have something to work with. – Tim Howey Oct 29 '15 at 19:31
  • @TimHowey updated my answer check both jquery and CF code – Keshav jha Oct 29 '15 at 19:44
  • I seem to be getting an error in firebug with that. "500 You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members." – Tim Howey Oct 29 '15 at 19:59
  • can you see the dump of getArray variable in cfc and you are using same index of getArray in your query? – Keshav jha Oct 29 '15 at 20:20
  • Ok, I made an error, and what you posted works for the example provided. However, when I attempt to pass a String as one of the array values, it breaks, even when returnType is set to "any" – Tim Howey Oct 29 '15 at 20:20
  • I've also made an attempt to change the variable to this. var mydata = new Array("1", "2", "3", "4", "5", "Tim"); What it looks like you did with the previous code is put everything in the form of a string, passed it, and then turned it into an array in the CFC. – Tim Howey Oct 29 '15 at 20:24
  • just updated jquery see edit. when you are passing string in json you need to wrap text in double quotes. your my data should look like this var mydata = '[1,"keshav",3]'; – Keshav jha Oct 29 '15 at 20:28
  • 2
    i have send the data in json format in form scope so that i can deserialize it and convert it into a array. cause you can not access or send javaScript array to CF. you need to send it as JSON and then convert it on server side. – Keshav jha Oct 29 '15 at 20:32
  • @TimHowey - Side note, is there a reason you must pass in an array specifically? Named arguments are intuitive and have better error handling support on the CF side. – Leigh Oct 30 '15 at 17:04