Chapter 28: AJAX ASP

AJAX + ASP (Active Server Pages / ASP Classic) example, explained as if I’m sitting next to you — a patient teacher who wants you to really understand every part.

We will go extremely slowly, step by step, with complete explanations, reasoning, common mistakes, security notes, and several complete runnable examples.

Before we start – what you need

To run these examples you need:

  • Windows computer
  • IIS (Internet Information Services) enabled (Control Panel → Programs → Turn Windows features on or off → Internet Information Services)
  • Classic ASP support enabled (Inside IIS → World Wide Web Services → Application Development Features → ASP)
  • Folder with write permission if you want to test file operations (not needed for basic examples)

Recommended folder structure:

text

Access via browser: http://localhost/ajax-asp-demo/index.html

Example 1 – Basic GET Request (fetch data from ASP)

File 1: index.html (the frontend – HTML + JavaScript)

HTML

File 2: get-data.asp (the backend – Classic ASP)

asp
<%@ Language=VBScript %>
<%
Option Explicit
Response.Buffer = True
Response.ContentType = "text/plain"
Response.CharSet = "utf-8"

' Prevent caching so we always get fresh data
Response.AddHeader "Cache-Control", "no-cache, no-store, must-revalidate"
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "Expires", "0"

Dim action
action = Trim(Request.QueryString("action"))

Select Case LCase(action)
Case "hello"
Response.Write "Hello from Classic ASP!" & vbCrLf
Response.Write "This message was sent via AJAX GET request." & vbCrLf
Response.Write "Current server time: " & Now() & vbCrLf

Case "time"
Response.Write "Current server date & time:" & vbCrLf
Response.Write Now() & vbCrLf
Response.Write "Server timezone: " & GetTimeZoneName()

Case "random"
Randomize
Dim num : num = Int((100 * Rnd) + 1)
Response.Write "Random number (1-100): " & num & vbCrLf
Response.Write "Generated at: " & Time() & vbCrLf

Case Else
Response.Status = "400 Bad Request"
Response.Write "Invalid or missing 'action' parameter." & vbCrLf
Response.Write "Valid values: hello, time, random"
End Select

' Small helper function
Function GetTimeZoneName()
GetTimeZoneName = "Server time zone not directly available in Classic ASP"
' In real project you would use registry or external component
End Function
%>

How to run Example 1

  1. Save both files in the same folder inside IIS (e.g. C:\inetpub\wwwroot\ajax-asp-demo\)
  2. Make sure Classic ASP is enabled in IIS
  3. Open browser: http://localhost/ajax-asp-demo/index.html
  4. Click the buttons → enjoy!

Example 2 – POST Request (Sending Form Data to ASP)

File: index-post.html

HTML

File: post-contact.asp

asp
<%@ Language=VBScript %>
<%
Option Explicit
Response.Buffer = True
Response.ContentType = "text/plain"
Response.CharSet = "utf-8"

Dim name, email, message

name = Trim(Request.Form("name"))
email = Trim(Request.Form("email"))
message = Trim(Request.Form("message"))

If Len(name) = 0 Or Len(email) = 0 Or Len(message) = 0 Then
Response.Status = "400 Bad Request"
Response.Write "Error: All fields are required."
Response.End
End If

If InStr(email, "@") = 0 Or InStr(email, ".") = 0 Then
Response.Status = "400 Bad Request"
Response.Write "Error: Invalid email address."
Response.End
End If

' In real application:
' - Save to database
' - Send email
' - Log activity
' Here we just simulate success with a delay

Dim i : For i = 1 To 800000 : Next ' fake 0.5–1 sec processing time

Response.Write "Thank you, " & Server.HTMLEncode(name) & "!" & vbCrLf & vbCrLf
Response.Write "We received your message:" & vbCrLf
Response.Write Server.HTMLEncode(message) & vbCrLf & vbCrLf
Response.Write "Email: " & Server.HTMLEncode(email) & vbCrLf
Response.Write "Received at: " & Now()
%>

Summary – What we covered

  • How to send GET requests with query parameters
  • How to send POST requests with form data (FormData)
  • How to handle loading states and errors
  • How Classic ASP receives GET (QueryString) and POST (Form) data
  • Basic security reminders (trim, encode output)
  • Real-world feel of AJAX + ASP Classic (still used in many legacy government, banking, ERP systems in India)

Next topics you may want:

  • AJAX + ASP returning JSON (modern style)
  • Live search / autocomplete with AJAX + ASP
  • File upload using AJAX + ASP
  • Loading spinner + disable button during request
  • Validation (client-side + server-side)

Tell me which one you want to practice next! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *