Using coldfusion and passing a structure of data to and from javascript functions

Posted on 2010-09-04
Last Modified: 2013-12-24

I have been learning javascript and am used to passing simple data to and from functions but now I would like to be able to send / receive more complex structures/arrays of data from and to coldfuson.
Can anyone shed any light on the best way to do this

Question by:Lmillard
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 13

Expert Comment

ID: 33602674

You can pass an object to a function, here is a bit about it.

You build an object that has attributes and then just pass it to a function, you can then pull attributes out of it as you please, its similar to passing an array.

LVL 63

Expert Comment

ID: 33603330
The format for complex datastructure is colled JASON.
ColdFusion speeks JASON:

LVL 29

Expert Comment

ID: 33604914
>>The format for complex datastructure is colled JASON. (sic)

Actually, it is called "JSON", which stands for "JavaScript Object Notation".

Visit for all things JSON.
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.


Expert Comment

ID: 33607344
Are you saying that you want to pass data from client side JavaScript functions into server side ColdFusion CFC's/functions, and receive data back?  If so, which ColdFusion version are you using?

Otherwise, can you explain a bit more?

Expert Comment

ID: 33608852
Using cfajaxproxy we can send and receive complex data to and from ColdFusion via CFC.Please refer

Author Comment

ID: 33609518
Apologies for the delay with this question,

Unfortunately I wont be able to use the cfajaxproxy as the code does need to be transposed to other languages so I am using standard jquery that will then post to either a cfc or .cfm page and then have a result returned. The result could be any format.

The code example below is a typical way that it is currently done but with the parameters being sent individually. I would prefer to be able to send all info as a structure.

function getBlogCats(pId,titleId) {
    $('#floatbox').html('<img src="../../Assets/Img/loading.gif" />')

The function is called by a simple <a onclick="getBlogCats(1,123)">View Blog Categories</a>

Open in new window


Accepted Solution

Gjslick earned 500 total points
ID: 33612244
Ok, what you want to do is first convert your data into a JSON string.  This will allow you to send complex objects/arrays via ajax (as there is no concept of 'object' or 'array' when it comes to HTTP; there is just plain text).  So to do this, you'll need a copy of Douglas Crockford's json2 script, as jQuery doesn't come with the ability to create a JSON string from an object.  You can get it here:  Just don't forget to remove the first line of the file.

Once you have that, and you have included it on your page along with jQuery, you will use the second argument to .load() to send the JSON string directly to the server as a POST parameter.  I usually just send it under the name 'data'.  Note that because this will be a POST parameter (not a URL parameter), you will retrieve it from the 'form' scope in ColdFusion. Here's an example:

<script src="jquery-1.4.2.min.js"></script>
<script src="json2.js"></script>
  function getBlogCats( pId, titleId ) {
    var data = {
      pId: pId,
      titleId: titleId

    $( '#floatbox' ).html( '<img src="../../Assets/Img/loading.gif" />' );
    $( '#floatbox' ).load( '../BlogManager/BlogCatSearch.cfm', { data: JSON.stringify( data ) } );

Note the use of JSON.stringify() to convert your data object into a JSON string.


Now in your .cfm file, you just need to de-serialize the JSON string into native ColdFusion structs/arrays.  Just for example purposes, try this:

<cfset data = deserializeJSON( )>
<cfdump var="#data#">

That should be all you need.  For other languages on the server side, you would use the JSON de-serializing function that is available for that language.  For example, in php, it's json_decode().

Author Closing Comment

ID: 33615489
Thanks for taking the time to put together a full example, this is exactly what I needed, perfect.

Expert Comment

ID: 33619757
Glad I could help!  

All the best,


Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question