AJAX with Classic ASP

AJAX lets a web page send an HTTP request to a server-side ASP endpoint and update part of the page without a full navigation. Classic ASP can return plain text, HTML fragments, or structured data; the browser-side request is made with JavaScript.

Older AJAX tutorials commonly build an XMLHttpRequest object and include Internet Explorer ActiveXObject fallbacks. Modern browsers provide the promise-based fetch() API, which is usually simpler for new front-end code. XMLHttpRequest still exists and may be encountered in legacy applications.

Simple ASP endpoint

<%
Response.ContentType = "text/plain"
Response.Write "Hello World"
%>

Call the ASP page with Fetch

<script>
async function sendRequest(event) {
  event.preventDefault();
  const output = document.getElementById("txtReturn");

  try {
    const response = await fetch("post.asp", {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body: new URLSearchParams({ source: "ajax-demo" })
    });

    if (!response.ok) throw new Error("HTTP " + response.status);
    output.textContent = await response.text();
  } catch (error) {
    output.textContent = "Request failed.";
  }
}
</script>

URLSearchParams correctly encodes name/value pairs for an application/x-www-form-urlencoded request. The code also checks response.ok; a Fetch promise does not automatically reject just because the server returns an HTTP error status.

HTML form and output area

<form id="ajaxForm" onsubmit="sendRequest(event)">
  <button type="submit">Send request</button>
</form>
<div id="txtReturn" aria-live="polite">Return message will appear here.</div>

When a response is plain text, assign it with textContent instead of innerHTML unless you intentionally return trusted HTML and have handled the associated injection risk.

The original two-file AJAX package remains available for learners maintaining older XMLHttpRequest-based code: download the legacy AJAX example ZIP.


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer