Does AJAX need to use a server-side language such as PHP/ASP.NET/Java to access a database? Or some sort of web service tied to these languages? Or is AJAX able to communicate directly with the database?

  • 2
    AJAX is not a language, it's a term for accessing a remote resource trough JavaScript without needing to reload the page. Knowing that, how would something that's not a programming language of any sort be able to access anything or perform any kind of task that you need, such as database access? Jun 16, 2011 at 14:05
  • Some databases, like Oracle, contain a heapload of features, some of them most likely would allow an Ajax application to directly fetch data from the database. That said, it doesn't seem to be a good idea to go that way.
    – user281377
    Jun 16, 2011 at 14:25

5 Answers 5


Ajax cannot access any database, since it's not a language, nor a precise technology.

Do you mean javascript? I don't think js can open a socket directly to your sql server. Anyway, that would require your db server to be open wide, which is a bad idea in most cases.

You could also use the localstorage api, if all you need is storing a limited piece of data clint side.


It can if the database engine itself has public endpoints (a webserver) built into it. I don't know of any off the top of my head but some are not far off. For example, you already communicate with MongoDb via Json. This question implies that there is some middle layers that you can just drop into place that will expose those endpoints via Http. Also you can probably use OData plugins.

In any case it isn't easy and it is definitely a bad idea - how would you do authentication? Are you going to allow anyone over the internet to push data directly into your database? To delete things?


AJAX is client side. You need a server-side connection to the database. This is mostly done in PHP, ASP.NET or Java.

It is possible to connect to a database from the client, for example like this:

<script language="JavaScript" > 

function getSubmit() 
var LastName; 
var Firstn = names.value ; 

var cn = new ActiveXObject("ADODB.Connection"); 
//here you must use forward slash to point strait 
var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:/clientDB.mdb"; 
var rs = new ActiveXObject("ADODB.Recordset"); 
//var SQL = "INSERT INTO Customers(FirstName, Surname)" 
//+ " VALUES ( '"+ names.value +"', '" + surname.value +"')"; 
var SQL = "select Surname, DOB from Customers where FirstName = '" + Firstn + "'"; 

rs.Open(SQL, cn); 
surname.value = rs(0); 
DOB.value = rs(1); 


But that will tell your visitors the username and password to the database. That's why a server side language is used.

  • 4
    Great example of what not to do! Jun 16, 2011 at 14:12
  • It's not pure Ajax, either. An activeX is required. Jun 16, 2011 at 15:03
  • 2
    Accepting SQL statements directly from a web client is just about the worst thing you could possibly do from a security standpoint.
    – Jeremy
    Jun 16, 2011 at 15:53

First of all, AJAX operates using HTTP protocol with JSON or XML payload. Most databases operate using proprietary protocols, with payload being SQL input, raw data as output.

Second, most web application provide security at application level. Giving direct access to the database would be huge security hole. With webapps, real security can only be achieved server side logic. In theory you could achieve this using views, stored procedures and having each webapp user as separate DB account. But that would be impractical, it's much easier and more efficient to do that with generic server-side language.


First off, note that AJAX stands for Asynchronous JavaScript And XML. Knowing that doesn't directly answer your question, but it is helpful.

Anyway, AJAX is an umbrella term for a certain use of a cluster of client-side technologies, so by definition it isn't accessing the server directly. I mean, the definition of the term is pretty much "using JavaScript in a webpage to communicate with the server". Although JavaScript itself can be used to program on the server, that is unusual and anyway the code is definitely still server-side (ie. JavaScript in the webpage is communicating with JavaScript on the server).

So the answer is that you don't access the database directly from client-side code and need code running on the server to access the database. The client-side code communicates with the server-side code using something like a RESTful API of GET/PUSH requests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.