I've blogged this before, but only as a side point. Since the question came in to me this morning I thought I'd mention it again in a new post. The question is simple - how do you return XML data in ColdFusion to be consumed by AJAX? (Or really any source.)
First off - if you want to return XML from a CFC method, you have to use returnType="xml". If you do not, the result will be WDDX encoded. Consider this method:
<cffunction name="test" returnType="string" access="remote" output="false">
<cfset var xml = "">
<cfxml variable="xml">
<root>
<people>ray</people>
<people>jeanne</people>
</root>
</cfxml>
<cfreturn xml>
</cffunction>
Viewed in your browser, the result is:
<wddxPacket version='1.0'><header/><data><string><?xml version="1.0" encoding="UTF-8"?><char code='0a'/><root><char code='0a'/><char code='09'/>
<char code='09'/><people>ray</people><char code='0a'/><char code='09'/>
<char code='09'/><people>jeanne</people><char code='0a'/><char code='09'/><char code='09'/></root></string></data></wddxPacket>
(Note, I added a line break or two so it wouldn't break the site layout.)
If you switch the returnType to XML, you then get:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<people>ray</people>
<people>jeanne</people>
</root>
So far so good. Before I go on - a quick note. You have to actually return an XML string or XML object. You can't just return a query and expect the returnType="xml" to convert it for you. That would be nice probably.
What if you don't have ColdFusion 7? First - buy it and mention my name! If you must use ColdFusion 6, create a ColdFusion page called proxy.cfm. This CFM file will call your CFC and then return the XML. Here is an example taken from BlogCFC:
<cfcontent type="text/xml">
<cfoutput>#queryToXML(entries, "entries", "entry")#</cfoutput>
In this case, I used a UDF to convert a query into an XML string. I use cfcontent to let the browser know XML is being returned. As a last step, I set up my AJAX front end to hit proxy.cfm instead of the CFC.
Archived Comments
Thats awesome actually I'm working on a project that this would work great for.
I do have one request can you show how to update a cf page through Spry with out reloading the page like on this page check your email i sent you the source code.
http://www.johnramon.com/ch...
Nod - I know. Need more time. :)
You can actually return XML in CFMX 6 without having a proxy.cfm file.
Set the returntype="void" and output="yes" then output the XML just as you would in a .cfm page.
cfcontent is still required. It is a bit hacky, but it works.
Of course, you can also use JSMX to digest the WDDX that a CFC method returns by default.
http://steve.coldfusionjour...
I'm still a big fan of using libraries that do all of this for you. I'm also very happy with JSON instead of XML, but that's just a preference. mxAjax is a good example; with that you really can just return a query from your CFC method and the whole thing is automatically turned into JSON ready to parse and use in the calling page.
Ray, are you a little surprised that Spry doesn't have any built in functionality to handle CFC-returned WDDX?
aaron@trajiklyhip.com has posted Andy Powell's Spry presentation to the Nashville CFUG.
One of the subtopics was returning XML.
http://www.ncfug.com/index....
BL - not really. People tend to forget about WDDX, but it was a dang cool way to do syndication before web services, and it is still darn handy for wrapping up complex logic.
Sorry - I meant complex data.
What I'm trying to get at is.. Why not make it easier for Adobe web developers (CFers)? Do you think the Spry team thinks it should be the CF team's responsibility to make it easier to integrate with Spry? Do you think Adobe is concerned about making it easier for Spry to integrate with CF, versus another server-side language?
To be fair - I think they are focusing on AJAX as a whole - not specifically stuff for CF. I'd like to see WDDX support, but it isn't critical to me.
Although I am sure it is not Adobe's priority, I agree with BL that Adobe should make it easier for CF to integrate with Spry, afterall interoperability between CF and other Adobe technologies can only serve to enhance CF as an important server-side language.
Anyway, the point of this moan is that Spry is not easy to integrate with at all and has little CF resource to help.
I have only been able to get Spry working when I create the XML using cfsavecontent in a cfm file! I cannot get a call to a cfc working or indeed via my fusebox framework! I'm sure I am missing something, but this frustration serves to weaken my opinion of Spry.
Has anyone integrated Spry with Fusebox, if so can you help?
thanks Raymond. this helped me a lot.
You are most welcome. Since this was useful, I'll go ahead and update the code formatting to make it nicer.