Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
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? –  Michael J.V. Jun 16 '11 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 '11 at 14:25

5 Answers 5

up vote 3 down vote accepted

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.

share|improve this answer

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 + "'"; 

cn.Open(strConn); 
rs.Open(SQL, cn); 
surname.value = rs(0); 
DOB.value = rs(1); 
//alert(rs(0)); 
rs.Close(); 
cn.Close(); 
} 
</script> 

source

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

share|improve this answer
4  
Great example of what not to do! –  FrustratedWithFormsDesigner Jun 16 '11 at 14:12
    
It's not pure Ajax, either. An activeX is required. –  Denis de Bernardy Jun 16 '11 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 '11 at 15:53

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?

share|improve this answer
    
There is CouchDB. –  Denis de Bernardy Jun 16 '11 at 15:06
    
There is a third-party RESTful interface for MongoDB called Sleepy Mongoose at snailinaturtleneck.com/blog/2010/02/22/…. –  Zhehao Mao Jun 16 '11 at 15:41

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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