AIR Tip 4 - Calling a SOAP Webservice
September 6th, 2007
Getting information from multiple locations on the Internet for a single application is commonplace these days. It is one of many things that makes AIR such a great application platform.
To begin with, we are going to be dealing with a very simple Coldfusion component. The component contains one function "getStuff". If you pass in your name, it will return a string that says "Your Name is ...". If you place this somewhere in your web tree, you can expose it as a webservice by adding "?wsdl" to the end of the filename. That is all you have to do to expose your Coldfusion Component as a SOAP Webservice.
Reference: Coldfusion 8 and Web Services
NOTE: I am planning to write an article on Consuming Coldfusion Webservices with JavaScript. This article will only cover the basics of it.
-
<cfcomponent>
-
-
<cffunction name="getStuff" access="remote" returnType="String">
-
<cfargument name="personName" required="true" type="string" />
-
-
<cfreturn "Your Name is " & personName />
-
-
</cffunction>
-
-
</cfcomponent>
Calling the Webservice is drastically different between Flex and Javascript. In Flex, you simply use the
-
<!--
-
WEB SERVICES
-
-->
-
<mx:WebService
-
id="sampleService"
-
wsdl="http://yourDomain/SoapTest.cfc?wsdl">
-
<mx:operation name="getStuff" result="onResult(event)" />
-
</mx:WebService>
-
import mx.rpc.events.ResultEvent;
-
-
private function callService(e:MouseEvent):void {
-
-
sampleService.getStuff.send(myName.text);
-
-
}
-
-
private function onResult(e:ResultEvent):void {
-
-
resultLabel.text = e.result as String;
-
-
}
With JavaScript, we are going to use the XMLHTTPRequest object just as we did in the last tip. Basically, we will have to add a couple of custom headers, and then craft the SOAP Envelope by hand.
Your reference for the SOAP Envelope will be the "wsdl" file. You can view the wsdl for our webservice in your browser by typing in the URL and adding "?wsdl" to the end of the file name. The wsdl is your guidebook to that specific webservice.
Reference: WSDL Tutorial
-
var xmlhttp;
-
var appXML;
-
-
function callService() {
-
-
var myName = document.getElementById("myName").value;
-
-
var url = "http://yourDomain/SoapTest.cfc?wsdl";
-
xmlhttp = new XMLHttpRequest();
-
xmlhttp.open("POST", url, true);
-
-
xmlhttp.onreadystatechange=function(){
-
-
if (xmlhttp.readyState==4) {
-
-
var mainDiv = document.getElementById('result');
-
mainDiv.innerHTML = xmlhttp.responseText;
-
-
}
-
-
}
-
-
xmlhttp.setRequestHeader("Content-Type", "text/xml");
-
xmlhttp.setRequestHeader('SOAPAction','http://yourDomain/SoapTest.cfc?wsdl');
-
-
xmlhttp.send("<?xml version='1.0' encoding='UTF-8'?>"+"\n\n"+
-
'<soapenv:Envelope'+
-
' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"'+
-
' xmlns:xsd="http://www.w3.org/2001/XMLSchema"'+
-
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
-
'<soapenv:Body>'+
-
'<ns1:getStuff xmlns:ns1="http://communications"'+
-
' soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'+
-
'<personName xsi:type="xsd:string">' + myName + '</personName>'+
-
'</ns1:getStuff>'+
-
'</soapenv:Body>'+
-
'</soapenv:Envelope>');
-
-
}
This is one clear example of something that is much easier with Flex. However, to make things easier with JavaScript, here are a few tips to remember:
- Case Matters. Be sure to watch your casing here - small inconsistencies will lead to errors.
- You can write your function outside of AIR so that you can use tools like Firebug for debugging.
- When calling a Coldfusion Webservice - if get the RDS Password page, you forgot to set the SOAPAction header.
- You have to use "POST" in the xmlhttp.open() function.
Flex Example
Source Code
JavaScript Example
Source Code
Coldfusion Component
Source Code



2 comments on “AIR Tip 4 - Calling a SOAP Webservice”
01
[...] AIR Tip 4: Calling a SOAP Webservice (AIR Beta 1) [...]
02
[...] 来源:AIR Tip 4: Calling a SOAP Webservice [...]
Leave a Reply