Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2010-09-04
Medium Priority
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 http://www.json.org for all things JSON.
Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control


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 2000 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: http://json.org/json2.js.  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( form.data )>
<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

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

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

Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

604 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