Skip to main content
December 17, 2009
Answered

Consuming a web service

  • December 17, 2009
  • 1 reply
  • 4300 views

Hello,

I'm trying to consume a web service that retrieves address data based on a zip code. I am using the following code to invoke the operation, but the output is cryptic:

Webservice: http://www.bronzebusiness.com.br/webservices/wscep.asmx?op=cep

<cfif IsDefined("Form.GetInfo")>
    <cfinvoke webservice="http://www.bronzebusiness.com.br/webservices/wscep.asmx?wsdl" method="cep" returnvariable="cepResult">
        <cfinvokeargument name="strcep" value="#Form.strcep#">
    </cfinvoke>
    <cfoutput>#cepResult#</cfoutput>
</cfif>

<form method="post" action="#cgi.script_name#" target="_self">
     <input type="text" name="strcep" /><input type="submit" value="sumit" name="GetInfo" />
</form>

Can someone figure out what I am doing wrong?

    This topic has been closed for replies.
    Correct answer -__cfSearching__-

    ahh of course :/ -- my bad!

    Well, I just tested this in Firebug, and the variables are coming back... the method ReturnAddress is executing correctly...

    {"COLUMNS":["STTYPE","STNAME","AREA","CITY","STATE"],"DATA":[["Rua","Sao Benedito","Santo Amaro","Sao Paulo","SP"]]}

    This is the response I'm getting under the Net tab... so the data is coming back correctly... but it's not referencing the fields correctly...

    This is the response I'm getting under the Net tab... so the data is coming back correctly...
    but it's not referencing the fields correctly...

    What is not referencing the fields correctly?  It looks fine. It should work correctly as long as you have modified the javascript function to use the correct field and query names.  Note - since it is javascript everything will be cAsE sEnSiTiVe.  So the query column names should be in all upper case and the form field id's exactly as they are in the <form>.

    <cfajaxproxy bind="cfc:NameOfYourCFC.ReturnAddress({strcep@keyup})" onsuccess="showData">

    function showData(d) {
        //convert into a struct
        var data = {}
        for(var i=0; i < d.COLUMNS.length; i++) {
            data[d.COLUMNS] = d.DATA[0]
        }
        document.getElementById('Area').value = data["AREA"];
        document.getElementById('City').value = data["CITY"];

        .... etcetera.....

    }

    1 reply

    Inspiring
    December 17, 2009

    but the output is cryptic:

    What is the input and output?

    December 17, 2009

    Here is the output: org.tempuri.CepResponseCepResult@104060f 

    The input is strcep, which is comes from my form, and calls the cep method in the webservice.

    If you want to test the service, use this sample zip code (called cep here): 04735005. The elements returned are UF: state, logradouro, which is type (st, ave, etc..), cidade: city, and nome: name of the st/ave. If you visit the webservice url and enter that cep to invoke the service, it works fine... but i can't return the data using cfinvoke.

    Inspiring
    December 17, 2009

    > Here is the output:
    > org.tempuri.CepResponseCepResult@104060f

    That is normal.  The web service response is translated into a java object. Normally, you can treat it like a structure to output the results by using the field names ie #responseVariable.someFieldName#.  But that web service seems to be using some sort of .net data set. Unfortunately, Axis (used to handle web service calls) does not handle those well. At least not from what I have read.

    You may have to extract the raw xml and parse it. Then you can access the elements.  I do not know much about that web service. So I am making some assumptions here. They may or may not be correct ;-) But this should get you started.

        <!--- get the raw responses (this is an array)--->
        <cfset responses = cepResult.get_any() />


        <!--- the second element seems to contain the actual results --->
        <cfif arrayLen(responses) gte 2>
            <!--- get the raw xml response and parse it --->
            <cfset xmlRoot = XMLParse(responses[2].getAsString())>
            <!--- search for the response element "tbCEP" --->
            <cfset nodes = xmlSearch(xmlRoot, "//tbCEP")>
            <!--- if it was found, display the results --->
            <cfif arrayLen(nodes)>
                Results: <br />
                <cfoutput>
                    logradouro = #nodes[1].logradouro# <br/>
                    nome = #nodes[1].nome# <br/>
                    bairro = #nodes[1].bairro# <br/>
                    cidade = #nodes[1].cidade# <br/>
                </cfoutput>

                Debug / Nodes Found: <cfdump var="#nodes#" label="Raw XML"/>
            </cfif>
       
        </cfif>