Ajax Asp in JavaScript
? Using AJAX with ASP (Classic ASP or ASP.NET) in JavaScript
You can use AJAX in JavaScript to interact with ASP pages on the server — just like you would with PHP or any backend language. Here’s how you can do it with Classic ASP and ASP.NET (Web Forms) using JavaScript AJAX.
?? 1. AJAX Request in JavaScript Using XMLHttpRequest
Here’s the basic structure of an AJAX request:
function loadData() { const xhr = new XMLHttpRequest(); xhr.open("GET", "data.asp", true); // "data.asp" is your Classic ASP file xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { document.getElementById("result").innerHTML = xhr.responseText; } }; xhr.send();}You can trigger this function with a button:
<button onclick="loadData()">Load from ASP</button><div id="result"></div>? 2. Classic ASP (data.asp) Example
This file returns a simple response:
<%Response.ContentType = "text/plain"Response.Write("Hello from ASP!")%>? 3. Sending Data to ASP with POST
You can also send data to ASP via POST.
? JavaScript (AJAX POST to ASP):
function sendData() { const xhr = new XMLHttpRequest(); xhr.open("POST", "process.asp", true); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); const data = "username=" + encodeURIComponent("john"); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { alert(xhr.responseText); } }; xhr.send(data);}? Classic ASP (process.asp):
<%Response.ContentType = "text/plain"username = Request.Form("username")Response.Write("Hello, " & username)%>? 4. Using AJAX with ASP.NET (Web Forms)
? JavaScript:
function getUserName() { const xhr = new XMLHttpRequest(); xhr.open("GET", "GetUser.aspx?userId=123", true); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { document.getElementById("userName").innerText = xhr.responseText; } }; xhr.send();}? ASP.NET Page (GetUser.aspx.cs):
// In code-behind file (C#)protected void Page_Load(object sender, EventArgs e){ string userId = Request.QueryString["userId"]; string userName = GetUserNameFromDatabase(userId); // hypothetical function Response.Write(userName);}?? Summary of What You Can Do with AJAX + ASP:
| Functionality | Technique |
|---|---|
| Load data from server | XMLHttpRequest, fetch() |
| Submit forms | AJAX POST with x-www-form-urlencoded |
| Update page content | Replace innerHTML dynamically |
| Communicate with ASP | Call .asp or .aspx files via AJAX |
Would you like a full working example of a login form or CRUD app using AJAX + Classic ASP?