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?
Ajax cannot access any database, since it's not a language, nor a precise technology.
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:
But that will tell your visitors the username and password to the database. That's why a server side language is used.
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.
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.