Tip

Creating JavaScript to access Web services

This Weblog describes the the process I follow when creating Dashboard OS X widgets that access Web services. I will not cover general widget creation, but the tools and process I follow to determine the format of the SOAP request that I need to recreate in JavaScript. This example should be useful for any JavaScript application, not just widgets.

Tools

For creating a JavaScript interaction with a Web service it is necessary to know the format of the SOAP request. For the masochists among us we could read the wsdl and work it out. However, I prefer to use the

    Requires Free Membership to View

SAP scripting tool add-on to Eclipse. I use this to generate a test PHP page for calling the Web service. Executing the test script provides you a copy of the request and response documents.

Scripting tool

Request/Response

The PHP test script shows you the request and response texts.

The request may require a SOAP Action. In this case it is possible to read it from the wsdl. However, as I am always running tcpdump for debug purposes I can check the outgoing tcp packet to get the SOAP Action from there.

Another tool used to test the requests is another widget I created. This widget allows me to send SOAP requests to services. Using this widget I can manipulate the request to test the service. The response is made available in the resultant scrollable text area (this widget will be covered in a future blog).

Implementation

Now that we have all the information needed, it is simply a case of making sure that the request is sent in the correct format and parsing the response. The code below contains the SOAP request for calling TH_USER_LIST and the XMLHttpRequest code.

function getSOAPEnvelope() { return '<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:sap-com:document:sap:rfc:functions"><SOAP-ENV:Body><ns1:TH_USER_LIST><LIST> <item><TID>0</TID><MANDT></MANDT> <BNAME></BNAME><TCODE></TCODE> <TERM></TERM><ZEIT></ZEIT><MASTER> </MASTER><HOSTADR></HOSTADR><TRACE>0</TRACE> <EXTMODI>0</EXTMODI><INTMODI>0</INTMODI> <TYPE>0</TYPE><STAT>0</STAT><PROTOCOL>0</PROTOCOL> <GUIVERSION></GUIVERSION><RFC_TYPE> </RFC_TYPE></item><item><TID>0</TID> <MANDT></MANDT><BNAME></BNAME> <TCODE></TCODE><TERM></TERM><ZEIT> </ZEIT><MASTER></MASTER><HOSTADR> </HOSTADR><TRACE>0</TRACE><EXTMODI>0</EXTMODI> <INTMODI>0</INTMODI><TYPE>0</TYPE> <STAT>0</STAT><PROTOCOL>0</PROTOCOL> <GUIVERSION></GUIVERSION><RFC_TYPE></RFC_TYPE> </item><item><TID>0</TID><MANDT></MANDT> <BNAME></BNAME><TCODE></TCODE><TERM> </TERM><ZEIT></ZEIT><MASTER></MASTER> <HOSTADR></HOSTADR><TRACE>0</TRACE> <EXTMODI>0</EXTMODI><INTMODI>0</INTMODI> <TYPE>0</TYPE><STAT>0</STAT> <PROTOCOL>0</PROTOCOL><GUIVERSION> </GUIVERSION><RFC_TYPE></RFC_TYPE> </item><item><TID>0</TID><MANDT></MANDT> <BNAME></BNAME><TCODE></TCODE><TERM> </TERM><ZEIT></ZEIT><MASTER></MASTER> <HOSTADR></HOSTADR><TRACE>0</TRACE> <EXTMODI>0</EXTMODI><INTMODI>0</INTMODI> <TYPE>0</TYPE><STAT>0</STAT> <PROTOCOL>0</PROTOCOL><GUIVERSION> </GUIVERSION><RFC_TYPE></RFC_TYPE></item><item> <TID>0</TID><MANDT></MANDT><BNAME> </BNAME><TCODE></TCODE><TERM></TERM> <ZEIT></ZEIT><MASTER></MASTER> <HOSTADR></HOSTADR><TRACE>0</TRACE> <EXTMODI>0</EXTMODI><INTMODI>0</INTMODI> <TYPE>0</TYPE><STAT>0</STAT> <PROTOCOL>0</PROTOCOL><GUIVERSION> </GUIVERSION><RFC_TYPE></RFC_TYPE> </item></LIST><USRLIST><item> <TID>0</TID><MANDT></MANDT><BNAME> </BNAME><TCODE></TCODE><TERM> </TERM><ZEIT></ZEIT><MASTER></MASTER> <TRACE>0</TRACE><EXTMODI>0</EXTMODI> <INTMODI>0</INTMODI><TYPE>0</TYPE> <STAT>0</STAT><PROTOCOL>0</PROTOCOL> <GUIVERSION></GUIVERSION><RFC_TYPE> </RFC_TYPE><HOSTADDR></HOSTADDR> </item><item><TID>0</TID><MANDT> </MANDT><BNAME></BNAME><TCODE> </TCODE><TERM></TERM><ZEIT> </ZEIT><MASTER></MASTER><TRACE>0</TRACE> <EXTMODI>0</EXTMODI><INTMODI>0</INTMODI> <TYPE>0</TYPE><STAT>0</STAT> <PROTOCOL>0</PROTOCOL><GUIVERSION> </GUIVERSION><RFC_TYPE></RFC_TYPE> <HOSTADDR></HOSTADDR></item> <item><TID>0</TID><MANDT> </MANDT><BNAME></BNAME><TCODE> </TCODE><TERM></TERM><ZEIT> </ZEIT><MASTER></MASTER><TRACE>0</TRACE> <EXTMODI>0</EXTMODI><INTMODI>0</INTMODI> <TYPE>0</TYPE><STAT>0</STAT> <PROTOCOL>0</PROTOCOL><GUIVERSION> </GUIVERSION><RFC_TYPE></RFC_TYPE> <HOSTADDR></HOSTADDR></item> <item><TID>0</TID><MANDT> </MANDT><BNAME></BNAME><TCODE> </TCODE><TERM></TERM><ZEIT> </ZEIT><MASTER></MASTER> <TRACE>0</TRACE><EXTMODI>0</EXTMODI> <INTMODI>0</INTMODI><TYPE>0</TYPE> <STAT>0</STAT><PROTOCOL>0</PROTOCOL> <GUIVERSION></GUIVERSION><RFC_TYPE> </RFC_TYPE><HOSTADDR></HOSTADDR> </item><item><TID>0</TID><MANDT> </MANDT><BNAME></BNAME><TCODE> </TCODE><TERM></TERM><ZEIT></ZEIT> <MASTER></MASTER><TRACE>0</TRACE> <EXTMODI>0</EXTMODI><INTMODI>0</INTMODI> <TYPE>0</TYPE><STAT>0</STAT> <PROTOCOL>0</PROTOCOL><GUIVERSION> </GUIVERSION><RFC_TYPE></RFC_TYPE> <HOSTADDR></HOSTADDR></item> </USRLIST></ns1:TH_USER_LIST> </SOAP-ENV:Body></SOAP-ENV:Envelope>'; } this.performSearch = function() { // Assemble a SOAP envelope var soapEnvelope = getSOAPEnvelope(); log(soapEnvelope); // Access the web service httpRequest = new XMLHttpRequest(); httpRequest.onreadystatechange = this.callback; with(httpRequest) { open('POST', self.service, true, gUserValue,gPassValue); // 'false' = synchronous, 'true' = asynchronous setRequestHeader('Content-Type', 'text/xml'); setRequestHeader('SOAPAction' , gActionValue); setRequestHeader('Connection' , 'close'); send(soapEnvelope); } };


This content is reposted from the SAP Developer Network.
Copyright 2006, SAP Developer Network

SAP Developer Network (SDN) is an active online community where ABAP, Java, .NET, and other cutting-edge technologies converge to form a resource and collaboration channel for SAP developers, consultants, integrators, and business analysts. SDN hosts a technical library, expert blogs, exclusive downloads and code samples, an extensive eLearning catalog, and active, moderated discussion forums. SDN membership is free.

Want to read more from this author? Click here to read John Astill's Weblog. Click here to read more about Scripting Languages on SDN.



This was first published in November 2006

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.