AJAX and Classic ASP

4.3 (73 votes)

Using the xmlhttpserver object in javascript to request an asp page containing script to output text.

UPDATE!! A new version of this article, using jQuery is available here.

Generic cross-browser js function to create an xmlhttpserver object. For a clear introduction to the basics of AJAX, this series of articles is particularly helpful: http://www.ibm.com/developerworks/web/library/wa-ajaxintro1.html.

function GetXmlHttpObject(handler)
var objXmlHttp=null;

if (navigator.userAgent.indexOf("MSIE")>=0)
var strName="Msxml2.XMLHTTP";
if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
objXmlHttp=new ActiveXObject(strName);
return objXmlHttp;
alert("Error. Scripting for ActiveX might be disabled");
if (navigator.userAgent.indexOf("Mozilla")>=0)
objXmlHttp=new XMLHttpRequest();
return objXmlHttp;

Example js function that is fired by a selected index change on an html SELECT element. This one recieves the CustomerID to append to the querystring of the asp script that will get executed. The first js function will call the second StateChanged function, which will output any returned string (responseText) to the innerHTML of a div.

function GetCustomer(id)
var url="FetchCustomer.asp?CustomerID="  + id ;

xmlHttp.open("GET", url , true);

function stateChanged() 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

The select list with the onChange event wired up to the GetCustomer function.

strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
strDb = "Data Source=" & Server.MapPath("Northwind.mdb")
strConn = strProvider & strDb
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open strConn
Set rs = Conn.Execute("SELECT [CustomerID], [CompanyName] FROM [Customers]")
If Not rs.EOF Then
  <select name="CustomerID" onChange="GetCustomer(this.value);">
  Do Until rs.EOF
    Response.Write "<option value=""" & rs("CustomerID") & """>" 
    Response.Write rs("CompanyName") & "</option>" & VbCrLf
  rs.Close : Set rs = Nothing : Conn.Close : Set Conn = Nothing 
  rs.Close : Set rs = Nothing : Conn.Close : Set Conn = Nothing
  Response.Write "<p>Something bad went wrong</p>"
End If
<div id="CustomerDetails"></div>

An example asp script that receives the value from the querystring and queries the database, outputting the result to the http Response, which is received by the xmlhttp object and changes its readystate property and fires the statechanged method, which writes the responseText to the div.

strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
strDb = "Data Source=" & Server.MapPath("Northwind.mdb")
strConn = strProvider & strDb
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open strConn
query = "SELECT * FROM Customers WHERE CustomerID = ?"
CustomerID = Request.QueryString("CustomerID")
arParams = array(CustomerID)
set cmd = Server.CreateObject("ADODB.Command")
cmd.CommandText = query
set cmd.ActiveConnection = Conn
Set rs = cmd.Execute(,arParams,1)
If Not rs.EOF Then
Response.Write rs(1) & "<br />" & _
  rs(4) &  "<br />" & _
  rs(5) & "<br />" & _
  rs(6) & "<br />" & _
  rs(7) & "<br />" & _
  rs(8) & "<br />" & _
  "Tel: " & rs(9) & "<br />"
End If
rs.Close : Set rs = Nothing
set cmd = Nothing 
Conn.Close : Set Conn=nothing

The sample above includes the use of a parameter array passed to the command's execute method. This helps protect against SQL injection.

Date Posted:
Last Updated:
Posted by:
Total Views to date: 77595


- Francisco

Gostaria de um Exemplo completo, não sei onde colocar essas rotinas. Sounovato.

- Mike


The first 3 code blocks go into the page that the user sees. The final block is a complete page.

- mehraveh

thank you. Its very good.

- Mike

This is one of the most popular articles on your site. Goes to show Microsoft that classic ASP is alive and well and, well, I for one love it still! Thanks!

- John


- usman

how to call a function after statechanged returns a value...
I actually have called ajax on mouse over ... and have to show the result in a small div.. which appears on mouse over ... the issue I am getting is the div appears before the ajax gets the responce and shows nothing if i put delay it gives the same problem. so how can i call a funciton inside state changed.... ????

- Paul Scannell

I'm implementing this because I have users that use other browsers besides IE. I'm only in the beginning testing phase, but I noticed that I can only run the code snippets once. I have to close the browser and reload it again in order to something a little different. I can't just do a refresh.

Is there a way to create the ajax connection once and then use it over and over with different quereis, etc.?

Recent Comments

Praveen 12/02/2016 14:22
In response to Migrating Classic ASP To ASP.NET Razor Web Pages Part One- Razor Syntax And Visual Basic
Nicely written article, just what I wanted to get me started, I am going to start working on a this...

Whitney W. 11/02/2016 15:37
In response to Adding A Controller
I am really new to everything and just started programming. I really need help in my project since I...

Fredrik 11/02/2016 13:10
In response to Request.Form Is Empty When Posting To ASPX Page
It worked. Thank you!...

David Valdez 11/02/2016 03:08
In response to Reading Excel Files Without Saving To Disk In ASP.NET
Muchas, muchas, gracias. Thank you so much from Dominican Republic....

Zahid 10/02/2016 00:42
In response to How To Send Email In ASP.NET MVC
Hello Sir, Great post. Just a quick question, is it possible if we can ask a client to save in an...

Al Wilton 07/02/2016 03:11
In response to Windows Authentication With ASP.NET Web Pages
I've been using this advise for quite a while. Today I was setting up a .NET 4.6.1 site and it drove...

Anders 06/02/2016 15:38
In response to iTextSharp - Working with Fonts
Thanks a lot for this excellent series on iTextSharp....

J_R 06/02/2016 02:45
In response to Simple File Download Protection with ASP.NET
Mike, Thank you for taking the time to write this. It really helped me - though I could not out a...

Nemat 04/02/2016 16:24
In response to Solved - The Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine
Installing Microsoft Access Database Engine 2010 64 bit helped me. Thanks A lot!...

Bill Barbour 03/02/2016 18:34
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Wonderful example. I have it all working. I would like to add the image to each row of the index you...