Ajax and rest are indeed two ways of using HTTP. But you can use both together.
HTTP (HyperText Transfer Protocol) is a protocol to send files (and data) from the server to the client. Originally it was intended to send static HTML files from the server to the client when the client's browser requested a (HTML) page. The HTML can contain hypertext links to other pages.
Later people started using HTTP to request the (HTML) results of programs that ran when client requested a file from the server (i.e. CGI, and later ASP and PHP). HTTP allows the client to post 'forms' to the server. The server CGI program could use the client's form to change the HTML page sent to the client.
Comparing this to the traditional CGI/ASP/PHP we see that instead of sending a web-form and waiting to receive a complete page in response, only the change parts of data are sent and they are sent in the background so that the page remains responsive to the user.
Rest is also a way of using HTTP. It's a reaction to some of the difficulties caused by the AJAX approach. In the AJAX approach all kinds of data is sent back and forth, all kinds of methods are called, the server expects the client to remember the data in both directions that was transmitted before, and the client expects the server to remember the same.
A difficulty is the the client and the server where heavily coupled to modify one, a programmer has to understand both. Finding which methods did what and which methods should be called when was quite a puzzle.
A second difficulty was scalability. Operations could not just add an other server or a proxy when the load became to much, since client and server assumed that the other party knew the data transmitted before. Switching requests to an other server in the middle of a session between a client and a given server, would require careful planning and carefully sharing the client's state among the servers, which complicated the server code.
REST returns to some of the original principles of HTTP that AJAX has abandoned.
- Don't use gazillions of different methods and ad-hoc error handling:
- use the methods available in the HTTP protocol (mainly GET, POST, PUT, and DELETE); and,
- use the HTTP status codes to signal various errors.
- Assume the server and the client are unaware of each others state and of data transmitted previously.
- Send all state required to handle a request with the request.
Just as in ye olden days when the server sent a complete web page when a client requested it and the forgot about the client.
- Structure the data as 'resources'3. With each being retrievable (and operable via POST and such) via an URL.
- This combines the send pages approach of traditional static HTML and dynamic CGI/ASP/PHP HTTP and the separate data from page (static content) approach of AJAX.
Items 1. and 3. helps reducing the puzzle of which method does what, what data is transferred, when each method should be called: any client that 'speaks' HTTP can connect to the server program. The client still has to know what resources the server publishes, the URLs of these resources, and their meaning (semantics).
This allows for simpler clients.
Item 2 allows for operational scalability, since the client and the server are unaware of each other's state and the previously exchanged requests. Switching a client mid-session to a different server is easier. Clustered servers do not have to exchange their clients' state among themselves.
This allows easier scalability and simpler server programs.
AJAX and REST work well together; you don't have to choose either one or the other:
- Separate a dynamic page into static content (from the server's viewpoint) and dynamic content (an idea from AJAX).
- Retrieve changing data in the background (an idea from AJAX).
- Keep the page responsive to the user while retrieving data (an idea from AJAX).
- Update the page in place when the client has received the data (an idea from AJAX).
- Structure the data as well defined resources (an idea from REST).
- Publish these resources via a well defined schema of URLs (an idea from REST).
- Use standard HTTP methods to retrieve and modify the data (an idea from REST).
- Use standard HTTP status codes to signal errors (an idea from REST).
- Assume the client and the server know nothing about each others state and the requests sent before
Transfer all state that is needed to handle the request in the request (and try to keep this to a minimum) (an idea from REST).
1 AJAX choose to use HTTP as its communication protocol because most webpages where already already transmitted via HTTP and all web browsers could already communicate via HTTP.
3 A REST resource is a piece of data that is well defined in the application.