TE Menu


Another blog on Lotus Notes Technologies

AJAX calling Web Service (How It Works)

John Turnbow  August 4 2008 08:31:35 PM
As promised, in a previous post I said I would explain how the AJAX solution I created works with a web service. I believe this is a nice solution as there is no additional software needed other than Lotus Notes.  Here is how I worked AJAX to call a Web service.  Simple, really, just call a Lotus Notes agent via XMLHTTP, the Notes agent then calls the Web service, utilizing the Consumer. The Web Service takes off on it's own could be anywhere on the web. The return value is then sent back to the agent for processing. The Lotus Notes agent does a simple print back to the XMLHTTP process which then uses some Javascript to print back to the web page.

Click Here for Sample

Here is a simple flowchart of how it works.
Image:AJAX calling Web Service (How It Works)
   Code from Lotus Notes Agent: (LookupData)
   Dim msg As String, username As String, password As String, endpoint As String
   Dim port As New ClassChemicals
   
   endpoint = "http://www.recondite2.com/Turnbow/chemicals.nsf/chemicalsList?openwebservice"
   username = "John Tester"
   password = "test"
   
   Call port.SetEndpoint( endpoint )  'Point to Web service with URL above
   Call port.SetCredentials(username, password ) 'send Name and password
   
   Returndata =  port.LookupChemical(getChemical) 'do the lookup here using web service - value is put into Returndata
   
   Print "Content-Type: text/plain"
   Print "Price is: " & Returndata & "  " & query 'passed back to XMLHTTP process.

Code from the Web Service.


We have two web services actually.  One as a consumer and one as a provider.  The "Provider" is the service that processes the data we pass it with the "Consumer". The Consumer makes a call to the Provider to give us the data we've asked for.

Provider

Dim s As NotesSession
Dim db As NotesDatabase
Dim nam As NotesName
Dim doc As NotesDocument
Dim view As NotesView
Dim dc  As NotesDocumentCollection

Class ClassChemData
     Public ChemicalName As String        
     Public Price As String
End Class

Class ClassChemicals
     Sub New()
             Set s = New NotesSession
             Set db = s.CurrentDatabase
             Set doc = db.CreateDocument 'temporary for Evaluate
     End Sub        
     
     Function LookupChemical(Chem As String) As String
             Set view = db.GetView("ByChemName")
             Set doc = view.GetDocumentByKey(Chem,True)
             If doc Is Nothing Then
                     LookupChemical = "Price Not Found"
             Else
                     LookupChemical =  doc.Price(0)
             End If
     End Function
     
End Class

Consumer

The Consumer is actually built for us by Notes when importing the WSDL.  

%INCLUDE "lsxsd.lss"
Class ClassChemicals As PortTypeBase
     
     Sub NEW
             Call Service.Initialize ("UrnDefaultNamespaceClassChemicalsService", _
             "ClassChemicalsService.Domino", "http://www.recondite2.com:80/turnbow/chemicals.nsf/chemicalslist?OpenWebService", _
             "ClassChemicals")
             
     End Sub
     
     Function LOOKUPCHEMICAL(CHEM As String) As String
             Let LOOKUPCHEMICAL = Service.Invoke("LOOKUPCHEMICAL", CHEM)
     End Function
     
End Class


So, now you can see how all the pieces fit together using a web browser with no added software.  Using AJAX, Javascript, a LotusScript agent you can interfaces with any Web Service.