So I was pretty annoyed how hard it was to find a description of how to return an xml document from a component back to an AJAX page that had sent a request to the component. I was making a simple form, and all I wanted was to make a simple call to a remoteService, and get a simple xml response. I didn’t really want an AJAX Framework/object for this simple task.
So after returning a simple string, the coldfusion service was adding the standard wddx xml tags around the string before it was received by the client page. So I learned you have to use a return type xml, instead of a return type string. But when I did that, even though the xml document looked well-formed, and how I had intended it to look with no wddx stuff, the receiving page was seeing null in the responseXML property of the XMLHttpRequest object.
So on this AJAX page, my XMLHttpRequest variable is called xmlHttp. And all I was getting was the xml doc as text in the xmlHttp.responseText property of the xmlHttp object. I wanted the xml doc to be in xmlHttp.responseXML and not the xmlHttp.responseText.
Using firebug (great FF add-on) I examined the reponse I was getting. Turns out the header of the response showed a content type of text/html and not an xml type. I finally figured out the key. In php yu could just add a header to set the content type to xml, using the header function. That is what I had been doing. Buth with coldfusion I had no idea how to add a content type header. THE KEY WAS IN THE CFCONTENT TAG. In my component being called, before I returned the xml object I had created, I learned you could just add a cfcontent tag to change the return type’s header.
<cffunction name="getSomethingInfo" access="public" output="false" returntype="xml">
<cfargument name="somethingID" type="string" required="true" />
<cfset var xml = "">
<cfquery datasource="adatasource" name="qThingInfo">
SELECT s2.*, u.* FROM Something s
INNER JOIN Something2 s2 on s2.userID = s.consultantID
INNER JOIN [User] u ON u.id = s2.userID
WHERE s.id = <cfqueryparam cfsqltype="cf_sql_integer" value="#somethingID#">
<cfif qThingInfo.RecordCount EQ 0>
<cfoutput query="qThingInfo" maxRows="1">
<!--- THIS IS KEY!!!--->
<cfcontent type="application/xml; charset=UTF-8">
<cfreturn xml />
My code to send the request using a coldfusion component:
+ cacheEntry , true);
// define the method to handle server responses
xmlHttp.onreadystatechange = handleRequestStateChange;
// make the server request
to call a coldfusion component and get a value from a method, the url for the request is: