JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

AJAX ASP Example

15 April 2025 | Category:

While ASP.NET is more common today, Classic ASP (using VBScript) is still used in legacy applications. Here, we’ll use AJAX to fetch data from a Classic ASP page without reloading the webpage.


📁 Project Files Structure

ajax-asp/
│
├── index.html         → Frontend with JavaScript
└── server.asp         → Classic ASP (VBScript) script for backend

📘 1. index.html – HTML + AJAX Request

<!DOCTYPE html>
<html>
<head>
  <title>AJAX with ASP Example</title>
</head>
<body>

<h2>Get Server Date (Classic ASP)</h2>
<button onclick="getDate()">Click to Get Date</button>
<p id="result"></p>

<script>
function getDate() {
  const xhr = new XMLHttpRequest();
  xhr.open("GET", "server.asp", true);

  xhr.onload = function () {
    if (xhr.status === 200) {
      document.getElementById("result").innerText = xhr.responseText;
    } else {
      document.getElementById("result").innerText = "Error loading data.";
    }
  };

  xhr.send();
}
</script>

</body>
</html>

đŸ§‘â€đŸ’» 2. server.asp – Classic ASP Code

<%
Response.ContentType = "text/plain"
Dim currentDate
currentDate = Now()
Response.Write "Server Date and Time: " & currentDate
%>

🔄 How It Works

  1. You click the button on the HTML page.
  2. JavaScript makes an AJAX GET request to server.asp.
  3. Classic ASP responds with the current date and time.
  4. The result is displayed instantly without refreshing the page.

✅ Output (Example)

Server Date and Time: 4/14/2025 5:47:12 PM

🧠 Notes

  • Make sure to run this on a server that supports Classic ASP (like IIS).
  • File extension must be .asp for server-side execution.
  • Classic ASP uses VBScript syntax and runs only on Windows-based servers.

💡 Bonus Idea

You can expand this to:

  • Get database values using ADODB.Connection
  • Send POST requests with AJAX and use Request.Form in ASP
  • Create login validation and return JSON (text format)