XML from CFC for XMLHttpRequest

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. CustomValidator and XMLHTTPRequest
      yes. "Anatoly" <anatolyr@gilat.com> wrote in message news:#feBwBqVDHA.2248@TK2MSFTNGP10.phx.gbl...
  3. #2

    Default 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

  4. #3

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139