{"id":11,"date":"2008-05-23T22:27:13","date_gmt":"2008-05-24T04:27:13","guid":{"rendered":"http:\/\/www.n8williams.com\/devblog\/?p=11"},"modified":"2008-06-09T22:39:02","modified_gmt":"2008-06-10T04:39:02","slug":"returning-an-xml-document-from-a-coldfusion-component-use-cfcontent","status":"publish","type":"post","link":"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent","title":{"rendered":"Returning an xml document from a coldfusion component &#8211; use cfcontent"},"content":{"rendered":"<p>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&#8217;t really want an AJAX Framework\/object for this simple task.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Using <a title=\"firebug\" href=\"https:\/\/addons.mozilla.org\/en-US\/firefox\/addon\/1843\" target=\"_blank\">firebug <\/a>(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&#8217;s header.<\/p>\n<div class=\"text-box\">\n<code>&lt;cffunction name=&quot;getSomethingInfo&quot; access=&quot;public&quot; output=&quot;false&quot; returntype=&quot;xml&quot;&gt;<br \/>\n\t&lt;cfargument name=&quot;somethingID&quot; type=&quot;string&quot; required=&quot;true&quot; \/&gt;<br \/>\n\t&lt;cfset var xml = &quot;&quot;&gt;<br \/>\n\t&lt;cfquery datasource=&quot;adatasource&quot; name=&quot;qThingInfo&quot;&gt;<br \/>\n\t\tSELECT s2.*, u.* FROM Something s<br \/>\n\t\tINNER JOIN Something2 s2 on s2.userID = s.consultantID<br \/>\n\t\tINNER JOIN [User] u ON u.id = s2.userID<br \/>\n\t\tWHERE s.id = &lt;cfqueryparam cfsqltype=&quot;cf_sql_integer&quot; value=&quot;#somethingID#&quot;&gt;<br \/>\n\t&lt;\/cfquery&gt;<br \/>\n\t&lt;cfif qThingInfo.RecordCount EQ 0&gt;<br \/>\n\t\t&lt;cfxml variable=&quot;xml&quot;&gt;<br \/>\n\t\t\t&lt;noResults&gt;<br \/>\n\t\t\t\t&lt;requestSubject&gt;SomethingInfo&lt;\/requestSubject&gt;<br \/>\n\t\t\t&lt;\/noResults&gt;<br \/>\n\t\t&lt;\/cfxml&gt;<br \/>\n\t&lt;cfelse&gt;<br \/>\n\t\t&lt;cfoutput query=&quot;qThingInfo&quot; maxRows=&quot;1&quot;&gt;<br \/>\n\t\t\t&lt;cfxml variable=&quot;xml&quot;&gt;<br \/>\n\t\t\t\t&lt;SomethingInfo&gt;<br \/>\n\t\t\t\t\t&lt;firstName&gt;#UrlEncodedFormat(firstName)#&lt;\/firstName&gt;<br \/>\n\t\t\t\t\t&lt;lastName&gt;#UrlEncodedFormat(lastName)#&lt;\/lastName&gt;<br \/>\n\t\t\t\t&lt;\/SomethingInfo&gt;<br \/>\n\t\t\t&lt;\/cfxml&gt;<br \/>\n\t\t&lt;\/cfoutput&gt;<br \/>\n\t&lt;\/cfif&gt;<br \/>\n        &lt;!--- THIS IS KEY!!!---&gt;<br \/>\n\t&lt;cfcontent type=&quot;application\/xml; charset=UTF-8&quot;&gt;<\/p>\n<p>\t&lt;cfreturn xml \/&gt;<br \/>\n&lt;\/cffunction&gt;<br \/>\n<\/code>\n<\/div>\n<p>My code to send the request using a coldfusion component:<\/p>\n<div class=\"text-box\"><code>xmlHttp.open(&quot;GET&quot;, &quot;RemoteServices\/ARemoteService.cfc?method=getSomethingInfo&quot;<br \/>\n+ cacheEntry , true);<br \/>\n\/\/ define the method to handle server responses<br \/>\nxmlHttp.onreadystatechange = handleRequestStateChange;<br \/>\n\/\/ make the server request<br \/>\nxmlHttp.send(null);<br \/>\n<\/code>\n<\/div>\n<p>to call a coldfusion component and get a value from a method, the url for the request is:<\/p>\n<div class=\"text-box\">\n<code>PathtoYourComponent\/YourComponent.cfc?method=yourMethodName&amp;<br \/>\nparam1=param1value&amp;param2=param2value<br \/>\n<\/code>\n<\/div>\n<p>Helpful Links:<br \/>\n<a title=\"http:\/\/www.coldfusionjedi.com\/index.cfm\/2007\/2\/8\/Returning-XML-in-ColdFusion-for-AJAX\" href=\"http:\/\/www.coldfusionjedi.com\/index.cfm\/2007\/2\/8\/Returning-XML-in-ColdFusion-for-AJAX\" target=\"_blank\">http:\/\/www.coldfusionjedi.com\/index.cfm\/2007\/2\/8\/Returning-XML-in-ColdFusion-for-AJAX <\/a><\/p>\n<p><a title=\"http:\/\/www.brucephillips.name\/blog\/index.cfm\/2006\/11\/11\/Use-ColdFusion-Function-Return-Type-of-XML-to-Provide-XML-to-Both-Spry-and-Flex \" href=\"http:\/\/www.brucephillips.name\/blog\/index.cfm\/2006\/11\/11\/Use-ColdFusion-Function-Return-Type-of-XML-to-Provide-XML-to-Both-Spry-and-Flex\" target=\"_blank\">http:\/\/www.brucephillips.name\/blog\/index.cfm\/2006\/11\/11\/Use-ColdFusion-Function-Return-Type-of-XML-to-Provide-XML-to-Both-Spry-and-Flex <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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, &hellip; <a href=\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Returning an xml document from a coldfusion component &#8211; use cfcontent&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.11 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Returning an xml document from a coldfusion component - use cfcontent - The Dev Pages<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Returning an xml document from a coldfusion component - use cfcontent - The Dev Pages\" \/>\n<meta property=\"og:description\" content=\"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, &hellip; Continue reading &quot;Returning an xml document from a coldfusion component &#8211; use cfcontent&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent\" \/>\n<meta property=\"og:site_name\" content=\"The Dev Pages\" \/>\n<meta property=\"article:published_time\" content=\"2008-05-24T04:27:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2008-06-10T04:39:02+00:00\" \/>\n<meta name=\"author\" content=\"Nate Admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@admin\" \/>\n<meta name=\"twitter:site\" content=\"@admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nate Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent#article\",\"isPartOf\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent\"},\"author\":{\"name\":\"Nate Admin\",\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\"},\"headline\":\"Returning an xml document from a coldfusion component &#8211; use cfcontent\",\"datePublished\":\"2008-05-24T04:27:13+00:00\",\"dateModified\":\"2008-06-10T04:39:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent\"},\"wordCount\":370,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\"},\"articleSection\":[\"Coldfusion\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent\",\"url\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent\",\"name\":\"Returning an xml document from a coldfusion component - use cfcontent - The Dev Pages\",\"isPartOf\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#website\"},\"datePublished\":\"2008-05-24T04:27:13+00:00\",\"dateModified\":\"2008-06-10T04:39:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/n8williams.com\/devblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Returning an xml document from a coldfusion component &#8211; use cfcontent\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/n8williams.com\/devblog\/#website\",\"url\":\"https:\/\/n8williams.com\/devblog\/\",\"name\":\"The Dev Pages\",\"description\":\"A knowledge base for web applications development (and beyond)\",\"publisher\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/n8williams.com\/devblog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\",\"name\":\"Nate Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g\",\"caption\":\"Nate Admin\"},\"logo\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/n8williams.com\",\"https:\/\/twitter.com\/admin\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Returning an xml document from a coldfusion component - use cfcontent - The Dev Pages","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent","og_locale":"en_US","og_type":"article","og_title":"Returning an xml document from a coldfusion component - use cfcontent - The Dev Pages","og_description":"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, &hellip; Continue reading \"Returning an xml document from a coldfusion component &#8211; use cfcontent\"","og_url":"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent","og_site_name":"The Dev Pages","article_published_time":"2008-05-24T04:27:13+00:00","article_modified_time":"2008-06-10T04:39:02+00:00","author":"Nate Admin","twitter_card":"summary_large_image","twitter_creator":"@admin","twitter_site":"@admin","twitter_misc":{"Written by":"Nate Admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent#article","isPartOf":{"@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent"},"author":{"name":"Nate Admin","@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757"},"headline":"Returning an xml document from a coldfusion component &#8211; use cfcontent","datePublished":"2008-05-24T04:27:13+00:00","dateModified":"2008-06-10T04:39:02+00:00","mainEntityOfPage":{"@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent"},"wordCount":370,"commentCount":0,"publisher":{"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757"},"articleSection":["Coldfusion"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent#respond"]}]},{"@type":"WebPage","@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent","url":"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent","name":"Returning an xml document from a coldfusion component - use cfcontent - The Dev Pages","isPartOf":{"@id":"https:\/\/n8williams.com\/devblog\/#website"},"datePublished":"2008-05-24T04:27:13+00:00","dateModified":"2008-06-10T04:39:02+00:00","breadcrumb":{"@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/returning-an-xml-document-from-a-coldfusion-component-use-cfcontent#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/n8williams.com\/devblog\/"},{"@type":"ListItem","position":2,"name":"Returning an xml document from a coldfusion component &#8211; use cfcontent"}]},{"@type":"WebSite","@id":"https:\/\/n8williams.com\/devblog\/#website","url":"https:\/\/n8williams.com\/devblog\/","name":"The Dev Pages","description":"A knowledge base for web applications development (and beyond)","publisher":{"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/n8williams.com\/devblog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757","name":"Nate Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g","caption":"Nate Admin"},"logo":{"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/n8williams.com","https:\/\/twitter.com\/admin"]}]}},"_links":{"self":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/posts\/11"}],"collection":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":0,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"wp:attachment":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}