AJAX connecting to MS Access DB and returning a Javascript Array

I need to be able call sql from a javascript page on a timed basis that brings back data from an Access db in a Javascript array that I can parse and put back on a website.   Does anyone have a simple fast example?
Thanks!
CW
Venom1234Asked:
Who is Participating?
 
recklezConnect With a Mentor Commented:
I will illustrate using asp.net but you could use anything you want. This is a working example.

client code: ajax.html
-------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
      
      
      
      
      var hostName = "www.yourdomainname.com";
      
      
      
            
      function AjaxObject()
      {
        var xmlhttp, complete = false;
            
            xmlhttp = GetXmlHttpObject();
            
        if (!xmlhttp) return null; //Failed to create XMLHttp Object
            
        this.Connect = function(url, method, parameters, fnCallback)
        {
            if (!xmlhttp) return false;
                  complete = false;
            method = method.toUpperCase();
      
            try
                  {
              if (method == "GET")
              {
                  xmlhttp.open(method, url+"?"+parameters, true);
                  parameters = "";
              }
              else
              {
                  xmlhttp.open(method, url, true);
                  xmlhttp.setRequestHeader("Method", "POST "+url+" HTTP/1.1");
                  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
              }
      
                        xmlhttp.onreadystatechange = function()
                        {
                  if (xmlhttp.readyState == 4 && !complete)
                  {
                    complete = true;
                    fnCallback(xmlhttp);
                  }
                        };
                        
              xmlhttp.send(parameters);
            }
                  
            catch(ex) { return false; }
            
                  return true;
        };
            
        return this;
      }

      function GetXmlHttpObject()
      {
            var theObject;
            
            try { theObject = new ActiveXObject("Msxml2.XMLHTTP"); }
        
            catch (ex)
            {
                  try { theObject = new ActiveXObject("Microsoft.XMLHTTP"); }
                  
                  catch (ex)
                  {
                        try { theObject = new XMLHttpRequest(); }
            
                        catch (ex) { theObject = false; }      
                  }
                  
            }
            
            return theObject;
      }
      
      function PostToServer()
      {
            var myConn = new AjaxObject();

            
            if (!myConn)
            {
                  alert("Your browser does not support AJAX");
                  return false;
            }
            
            var fnWhenDone = function (oXML)
                                     {
                                           var temp = oXML.responseText.split("|");
                                           ProcessResponse(temp);                                                                  
                                     };
                                                             
            var qs = "q=your_query" //Data to post to server (otpional)            
            var postURL = "http://" + hostName + "/getresponse.aspx" //send data to server with POST
            myConn.Connect(postURL, "POST", qs, fnWhenDone);
      }
      
      function ProcessResponse(returnArray)
      {
            var temp = new String();
            
            for (i=0; i < returnArray.length; i++)
                  temp += "Value " + i + ":" + returnArray[i] + "<br />";
            
            document.getElementById("response").innerHTML = temp;
      }
</script>
</head>
<body>
AJAX Example<br /><input name="" value="Test Me" type="button" onclick="javascript:PostToServer();" /><br /><br />
<div id="response"></div>
</body>
</html>



server code: getresponse.aspx
-----------------------------------

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="utf-8" %>
<%@ Import Namespace ="System.Data" %>
<%@ Import Namespace ="System.Data.OleDb" %>
<%
Dim result As String =""

'*************************
'*************************
'*************************
'REMOVE THE FOLLOWING LINES AND REPLACE THE CONNECTION
'STRING AND SQL STATEMENTS TO WORK WITH YOUR SET UP
result = "Joe|Tom|Evan|Gloria|Vicky|George"
Response.Write(result)
Response.End()
'*************************
'*************************
'*************************

Dim oCn as OleDbConnection
Dim oCmd as OleDbCommand
Dim oDr as OleDbDataReader
Dim connStr As  String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + Server.MapPath("database.mdb")
Dim SQL As String =""

oCn = new OleDbConnection(connStr)
oCn.Open

SQL = "SELECT * FROM USERS;"

oCmd = New OleDbCommand(SQL)
oCmd.Connection = oCn
oCmd.CommandText = SQL

oDr = oCmd.ExecuteReader


If oDr.HasRows Then
      While oDr.Read
            result += oDr.Item("first_name")
      End While
End If

oDr.Close()
oCn.Close()
oCmd.Dispose()
oCn.Dispose()

Response.Write(result)
%>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.