Skip to main content
Known Participant
November 11, 2008
Question

Best practices for submitting CF data to an AJAX page?

  • November 11, 2008
  • 9 replies
  • 1116 views
Hi everyone,
I've got a project I'm working on for work and have hit a little problem.

I am extracting data from my database and then after each piece of data (just numbers, usually 10 chunks of numbers), I tack a "|" onto the end of each number. Then, I output the data to the page. Back on my AJAX enabled page, I get the "responseText" from that page, and then split it up using javascript and the pre-inserted "|".

This seems to work fine, but it is quite a bit more messy. Also, It would really, really be nice to be able to do sorting and various other operations on the data with javascript instead of having to rely on CF's icky code logic.

Can someone please enlighten me as to best practices for this type of thing? I get the suspicion that I'll probably be using XML somehow, but I'd your opinion.

Thanks!
This topic has been closed for replies.

9 replies

Known Participant
November 14, 2008
Another question, what is the purpose of the "variables" in "variables.xmldata"? also, reset?
Inspiring
November 14, 2008
quote:

what is the purpose of the "variables" in "variables.xmldata"?

This explicitly places the xmldata variable in the local variables scope, as opposed to request, application, etc. Using scoped variables is optional.
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Variables_30.html#1123172

quote:

reset?

See cfcontent documentation
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_c_11.html#2850760
Known Participant
November 14, 2008
Yeah, I was wondering and thinking something was fishy with my CF. Okay, so I'm invoking the method, dumping some information. Makes sense...and.....with a little tweaking...

I GOT IT TO WORK!
thank you so much!
Inspiring
November 14, 2008
You could also wrap your cffunction inside a cfcomponent tag and save this as *.cfc file to make the function accessible from a URL.

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_c_10.html#3350776
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=buildingComponents_01.html#1266855
Known Participant
November 14, 2008
That's it.... :(
Inspiring
November 14, 2008
You need some code on xmlout.cfm to call your function and return the results. See quick and dirty attached code. If you browse to xmlout.cfm you should now see the XML. I haven't checked your Javascript though.
Known Participant
November 13, 2008
Nuts. Unfortunatley, I'm not using DW CS3, I'm actually using DW 8. So using spry is icky.
Is there a crash course on outputting XML from Coldfusion to an AJAX page?
Basically, I just need some way to group my data together so instead of doing a very rudimentary way of inserting a "|" between each piece of data (spareee me!), I can send a large piece of information back to my page for simpler and cleaner processing.
I've just never really quite understood how to actually wrap up data in XML and send it back to the page...that part is simply mystifying, after hours of research..so it think I'm going to go the XML way... Bob, you've been a huge help, hopefully you can come through once more for me :)
Inspiring
November 13, 2008
You could write your own Javascript to handle the calls to the server and outputting the results to your page, but I recommend against re-inventing the wheel when using an existing Javascript framework can save time.

Note that spry is simply a collection of javascript files. Dreamweaver is not required to use Spry. Take a look at Ray Camden's Spry presentation using Spry + ColdFusion.

https://admin.adobe.acrobat.com/_a200985228/p92687048/

Known Participant
November 14, 2008
I watched the video and I still simply don't quite get it.
I just can't seem to catch a break here. I've to this date never been able to return XML (or JSON for that matter) from a CF page to an AJAX app that would be able to read it....I've gone the XML route, and here is what I have so far, it is a CF file outputting XML and a CF file retreiving it usig AJAX. I simply do not understand how this does not work

Known Participant
November 13, 2008
Sorry for the delayed response, I haven't been at work for a couple of days. I downloaded and (semi-installed...?) Spry, and It would really help if you could direct me to some material to how I can find out how to put my data into JSON on the CF page, and then how to access it with JS. I've searched around, but I can't seem to get a definitive source on the subject.
Thanks!
Inspiring
November 13, 2008
Check out the Samples and Documentation portions of Adobe's Spry website for client side use of JSON with Spry.
http://labs.adobe.com/technologies/spry/home.html


Here is link to Adobe's Spry Forums:
http://www.adobe.com/cfusion/webforums/forum/categories.cfm?forumid=72&catid=602


If you are using CF8 you can use the SerializeJSON function to convert a variable to JSON. You might also be interested in the cfsprydataset tag. CF 8 documentation:
http://livedocs.adobe.com/coldfusion/8/htmldocs/


If you are using a previous version of CF there is 3rd party JSON support. You can find links at http://json.org.
Inspiring
November 13, 2008
CF 8 also added a returnformat attribute to cffunction that could be used to return JSON from a CFC.
Known Participant
November 11, 2008
I don't know what Javascript framework I'm using...I'm simply using <script> tags.
I took a good look at JSON and it seems like a very viable solution for my problems.
However, in terms of actual implementation, I don't quite know where to start. At the moment, it seems I have to write my own parser or somehow download a pre-done parser because JSON support isn't built into Javascript.
Could you enlighten me?

Thanks!
Inspiring
November 11, 2008
A Javascript framework can make your life easier by providing a pre-built set of libraries and APIs to accomplish common tasks such as AJAX calls, form validation, table sorting, etc.

I recommend jQuery and Spry, but there are plenty of others.

jQuery
http://jquery.com/

Spry, note that Dreamweaver and CF8 both have built-in support for Spry.
http://labs.adobe.com/technologies/spry/

Spry demo by Ray Camden
https://admin.adobe.acrobat.com/_a200985228/p92687048/


If you want to write your own AJAX handling code you might want to use the library from json.org.
http://www.json.org/js.html

Edit: Corrected link and spelling.
Inspiring
November 11, 2008
freedomflyer,

What Javascript framework, if any, are you using?
Inspiring
November 11, 2008
Take a look at JSON (JavaScript Object Notation). XML is also a good option.

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=ajaxdata_09.html