?
Solved

AJAX connecting to MS Access DB and returning a Javascript Array

Posted on 2006-06-04
3
Medium Priority
?
296 Views
Last Modified: 2010-07-27
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
0
Comment
Question by:Venom1234
1 Comment
 
LVL 4

Accepted Solution

by:
recklez earned 500 total points
ID: 16842847
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question