The XMLHTTP object allows one to open an HTTP connection to a server, send some data, and get some data back, all in a few lines of script. The data exchanged through the XMLHTTP object is usually XML, but it can also be in plain text or binary data. The binary data is particularly useful when uploading files.
The XMLHTTP object can be included on a Notes Form in the JS Header section as a function or directly in the onLoad event of the form. This can also be event driven, i.e., it can be inserted behind a button or image and invoked on click of the respective event handler.
To initialize the object, create a function,
say 'loadXMLObj()' in the JSHeader of a form.
Function definition
function loadXMLObj()
{
var xmlHttp = new ActiveXObject
("Microsoft.XMLHTTP") [Line1] for IE
var xmlHttp = new XMLHttpRequest();
[Line1] for Netscape
xmlHttp.open("GET", "/HTTPAgent?
openagent", false) [Line 2]
xmlHttp.send() [Line 3]
xmlStr=xmlHttp.responseText [Line 4]
}
[Line 1] is the first step wherein you
initialize the XML object.
In [Line 2], an HTTP Request is
sent to the server.
The server in the example above is
the default Domino server.
The request is like an agent trigger.
In [Line 3], the Request is sent to the server.
Finally in [Line 4] the Response is
retrieved and it is in the form of plain text.
The response can be in the form of an
XML object in which case you will use:
xmlDoc=xmlHttp.responseXML
(The XMLDoc in this case is an XML object)
Some of the important properties
that the XMLObject supports are
listed below.
readyState – returns the
loading state of the XMLHTTP object
reponseText – returns the
request of the object either as a
string,XML object or binary data
status – returns the status
of the operation as a code
statusText – return the status
of the operation as a string
The above properties of the XMLHTTP
object are particularly useful in order
to determine
whether the connection to the remote
server was established successfully or not.
Finally in the onLoad event of the form,
include the function call 'loadXMLObj()'
Now let us proceed to the agent.
Agent Header
In the beginning of the agent, include
the following statement:
Print "Content-Type:text/plain"
[Note: This is used if the response
must be a string]
(or)
Print "Content-Type:text/xml"
[Note: This is used if the response
must be in XML format]
Agent Body
Here you may include native agent code like-
Dim session as new notessession
Dim db as notesdatabase
…….
[Proceed to initialize all required
doc handles] ……..
Let us say the requirement is to return
a field value from a collection of documents.
While not doc is nothing
Print doc.FieldName(0) [Response statement]
Set doc = DocColl.getNextDocument(doc) Wend
The line marked in Bold is the key command
wherein the agent 'Response' is sent to the
XMLHTTP Object.
This is done using the Print statement.
That's all there is to it. How the agent can be used is left to the Business Requirement.
You can connect to an RDBMS, any third-party application and send the response to the client without page refresh.
The response time is also much faster compared to CGI methods.
Besides agents, you can also use this object to load an XML file and traverse the tree using the XMLDOM.
To use this as a substitute for a
@Dblookup, you can send the request
as a 'readviewentries' view command
and the response can be obtained
in the form of a string.
For example:
var xmlHttp =
new ActiveXObject("Microsoft.XMLHTTP")
xmlHttp.open("GET", "/SampleView?
readviewentries&startkey=keyvalue",
false) [Sorted view]
xmlHttp.open("GET", "/SampleView?
readviewentries&restricttocategory=category",
false) [Categorized view]
xmlHttp.send()
xmlStr=xmlHttp.responseText
Do you have comments on this tip? Let us know.