Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.
-
ZapBrannigan #1
XML from CFC for XMLHttpRequest
Hi all:
Let me prefix my question with the statment that I have searched the forums
and found the answers given there to not be the solution to my problem.
I am trying to get a CFC to return a string representation of an XML document
to Javascript's XMLHttpRequest. So far Javascript has failed to recognize the
string as XML even though the formatting is correct. I have tried <cfxml> tags
and I have tried letting ColdFusion handle the formatting by using the XmlNew()
route. When I dump the returned text into the body of the page, the XML
formatting seems to be correct, save that there are no carriage returns. The
problem seems to be that the XMLHttpRequest object does not know what to do
with the returned values. Everything works properly when I have XMLHttpRequest
directly request an .xml file, but the ColdFusion route has not worked yet.
I am hoping that I've just forgotten to do something; "I'm a fool most times
and usually so". This all makes me irritated that out of all the things
ColdFusion can do with XML, it cannot simply return an xml document (in 6.1
anyway). That's quite an oversight. Anyhow, the code below is what I have
tried:
<cfcomponent output="no" displayname="PCA to XML">
<cffunction name="getPCAs" access="remote" returntype="string">
<cfquery name="pcas" datasource="PCA_MILESTONE">
SELECT PCA_NUMBER FROM PCA_TABLE
</cfquery>
<cfset pca_list = XmlNew()>
<cfset pca_list.XmlRoot = XmlElemNew(pca_list, "pcas")>
<cfset i = 1>
<cfloop query="pcas">
<cfset pca_list.pcas.XmlChildren[i] = XmlElemNew(pca_list, "pcaNum")>
<cfset pca_list.pcas.XmlChildren[i].XmlText = "#pcas.PCA_NUMBER#">
<cfset i = i + 1>
</cfloop>
<!--- Also tried this
<cfxml variable="pca_list" casesensitive="yes">
<pcas>
<cfoutput query="pcas"><pcaNum>#PCA_NUMBER#</pcaNum></cfoutput>
</pcas>
</cfxml>--->
<cfset xml = ToString(pca_list)>
<cfreturn xml>
</cffunction>
</cfcomponent>
<!--- and the Javascript --->
var req;
function loadXMLDoc(url)
{
//mozilla based browser implementation
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
}
//ie based browser implementation
else if (window.ActiveXObject)
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (req)
{
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
}
}
function processReqChange()
{
//only if req shows complete
if (req.readyState == 4)
{
//only if "OK"
if (req.status == 200)
{
response = req.responseXML;
items = response.getElementsByTagName('pcas');
//alert below always returns 0 even though there is a root element
// I have setting the tag name above to a child
element with same result: 0
alert(items.length);
} else {
alert("There was a problem retrieving the XML data: \n" + req.statusText);
}
}
}
ZapBrannigan Guest
-
XMLHTTPRequest, is it possible?
For now I simply want to know if its possible. I want to write an extension, that when a user clicks on a button it requests a web page from... -
CustomValidator and XMLHTTPRequest
yes. "Anatoly" <anatolyr@gilat.com> wrote in message news:#feBwBqVDHA.2248@TK2MSFTNGP10.phx.gbl... -
fwscott #2
Re: XML from CFC for XMLHttpRequest
Hi Zap,
How are you calling your loadXMLDoc() function?
If you are calling it by something like
loadXMLDoc('http://www.mydomain.com/mycfc.cfc?wsdl');
Then the XML document returned will only be the description of the webservice.
You can verify this by doing an alert(req.responseText) in your
processReqChange() function.
I think in order to use a CFC and javascript you need to form a soap request
to pass to your loadXMLDoc() call.
Perhaps [url]http://www.devarticles.com/c/a/ASP/HTTP-Tunneling-Revealed-Part-2/2/[/url]
will help some.
Or, you can move your code to a regular cfm file and make sure you put
<cfcontent type="text/xml">
at the top of the page.
fwscott Guest
-
ZapBrannigan #3
Re: XML from CFC for XMLHttpRequest
Thank you for the reply. I switched to a .cfm file and set the content type as
you described and it works. Thank you very much fwscott. I can't believe such
a headache for something so small, but that's the way it usually goes I guess.
Thanks again.
ZapBrannigan Guest



Reply With Quote

