Invoking XMLHTTP from Client Side. |
Written on: Dec, 5th 2001. |
Introduction
When you are working with Classic ASP, their might have been many occasions in which you
have invoked a server side script to pull data from a database and then filled data in
browser with the data pulled back. In this process, the current page should have been
refreshed, since we sent a request to IIS to process an ASP. When you paint the screen
with the data, you might have wrote so many lines of code to maintain the previous state
of all the objects. Aren't you tired of this. How about invoking an ASP page from the
client side and filling the page with the data given back by the ASP page without
refreshing the current screen? Following article discusses this. And this can be done
only from a IE browser 5.0 and above.
Let me break the points in the above question.
a) We should invoke an ASP page from Client Side
b) The ASP page will be returning some data taken from a database
c) This data should be used in the current page
d) and the most important, The current page in browser should NOT BE REFRESHED.
A perfect example would be as follows. Assume that, you have a Select box filled
with Order numbers. When you select any order number, you need to display the order
details. Order details are stored in the database. So, in usual case, we will
call a ASP or submit to the same page, which executes a query to pull out order
detail values. We may need to re-paint the current page in browser once again.
For this we have to write many lines of code.
We can achieve this target by using XMLHTTP.
XMLHTTP object is designed to pass XML documents to programs over the Internet using the
standard HTTP protocol. You have the XMLHTTP or XMLHTTPRequest object installed on your
computer if you've installed any version of Internet Explorer 5.0 or better. You can also
download the current version from http://msdn.microsoft.com/xml.
The following discussion is not a tutorial on XMLHTTP object. We have lots of
URLs for this. We will see how can we use XMLHTTP from client side. Assume that, we
have following page in the browser.
Our aim is to display Order details for the selected order number. In OnClick event of
the button, "Order Details" we should invoke an ASP page, which in turn will pulls out
order details from the database. We are going to invoke an ASP page called
"GetOrderDetails.Asp" using XMLHTTP from the client side.
| OnClick event for the button "Order Details" |
| <script language=vbscript> |
| |
| function GetOrderDetails(intOrderID) |
| |
| Set oXML = CreateObject("Msxml2.XMLHTTP") |
| |
| oXML.Open "GET", "GetOrderDetails.Asp?id="&intOrderID, False |
| oXML.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" |
| oXML.Send |
| |
| msgbox oXML.responseText |
| GetOrderDetails = Cstr(oXML.responseText) |
| |
| Set oXML = nothing |
| end function |
| |
| </script> |
In the above example, we invoke a client side method called GetOrderDetails. This method
accepts a parameter, OrderID, which is then passed to the GetOrderDetails.Asp. We
use XMLHTTP to invoke the ASP page and we pass the OrderID as a querystring to the ASP
page. The output is received in the property called responseText. Now, we will see the
ASP page which extracts order details from the database.
| GetOrderDetails.Asp |
| <% |
| |
| strXML = "<?xml version='1.0'?>" |
| strXML = strXML + "<OrderDetails>" |
| strXML = strXML + "<Order ID='" & Request.QueryString ("id") & "'>" |
| |
| set con = Server.CreateObject ("adodb.connection") |
| con.Open "driver=sql Server;Database=ASPAlliance;Server=myserver;uid=sa;pwd=;" |
| |
| set rst = con.Execute ("sp_getOrderDetails " & Request.QueryString ("id")) |
| do while not rst.EOF |
| strXML = strXML + "<field1>" & rst("field1")& "</field1>" |
| strXML = strXML + "<field2>" & rst("field2")& "</field2>" |
| strXML = strXML + "<field3>" & rst("field3")& "</field3>" |
| rst.movenext |
| loop |
| |
| strXML = strXML + "</Order>" |
| strXML = strXML + "</OrderDetails>" |
| |
| Response.Write strXML |
| |
| set con = nothing |
| %> |
Above ASP page is a very simple one. It just executes a Stored Procedure and builds an
XML string. This XML string is returned back to the called page. The statement
Response.Write strXML is actually returning the string back to the client side.
So, in the first example, we have a following statement msgbox oXML.responseText.
This responseText receives what ever the GetOrderDetails.Asp writes using Response.Write
Test it!
Test the Example
Summary
Thus, we have displayed the order details as a XML String within Client Side. You may
need to traverse the XML string to display the Order Details in a eligible format. Here
the Screen is not refreshed. When the user clicks on "Order Details" button, we hit
a database and displays the result. XMLHTTP is fast and as data is passed through XML, it
is very easy to handle the output. And in my coming article, We will discuss how to parse
through a XMLString and creating HTML elements with the help of VBScript (in client side).
XML Parsing and building dynamic HTML Elements
Links
http://www.4guysfromrolla.com/webtech/110100-1.shtml
http://www.15seconds.com/issue/991125.htm
http://www.aspalliance.com/das/xmlparse.aspx
Send your comments to das@aspalliance.com
Back to Article list
|