14

On my web page, if I wish to display dynamic dropdowns (e.g city names based on country selected), I can do it using AJAX. But I can also do it using a REST call. So which should I use?

My problem is I really don't get the difference between REST and any other HTTP browser request.(a.k.a a form submit). I have looked at the formal definitions of REST and it seems identical to a HTTP request. So how is REST inherently different from AJAX?

4
  • 6
    As you guessed, they are not mutually exclusive at all. Jul 25, 2013 at 4:46
  • 2
    you would usually use a REST API when your application is accessed by another application Jul 25, 2013 at 5:02
  • I don't see the comparsion between the two. AJAX is an application of several techinques to implement dynamic web content, while REST is an architectural style.
    – jramoyo
    Jul 25, 2013 at 9:58
  • 2
    @downvoter---why the hate?
    – Kaushik
    Jul 25, 2013 at 13:29

3 Answers 3

27

I can do it using AJAX. But I can also do it using a REST call.

Um, no. Those two are completely orthogonal. If you want to update your page with data you have to get from a server, you will do it using AJAX. There is no other way. And that AJAX call can use REST, or something else.

My problem is i really dont get the difference between REST and an HTTP browser request.(a.k.a a form submit). I have looked at formal definitions of REST and it seems like a HTTP request.

A REST call is an HTTP request, always. Though it can be used to handle regular browser calls (like form submits) and return full HTML pages, it's usually used to handle API calls that return only data (usually in JSON format).

So why it has a separate name?

Because REST is a specific style of using HTTP, arguably using it as it was originally meant to be used, but which most people didn't "get" and was thus rarely used for almost 2 decades.

Specifically, REST means encoding which entity you want to retrieve or manipulate in the URL itself (usually via an ID) and encoding what action you want to perform on it in the HTTP method used (GET for retrieving, POST for changing, PUT for creating, DELETE for deleting).

2
  • 6
    REST does not have to always be over HTTP. Any protocol may be REST if it fits the set of methods. But in web application context it will be HTTP, of course.
    – Jan Hudec
    Jul 25, 2013 at 8:25
  • 10
    @Jan Hudec: Are there any real world examples of REST being used with other protocols? Jul 25, 2013 at 8:26
7

Well I think you first need to understand that AJAX and REST are not really to alternatives for your use case. AJAX stands for asynchronous javascript and XML so if you are using javascript to load data after the browser request has finished you are doing AJAX.

REST on the other hand stands for Representational State Transfer which as Stefan Billet pointed out uses HTTP requests to transfer data. So REST is actually one way to do AJAX with. SOAP would be another one but this is offtopic for your question. So your question should rather be: "Which technology is best for my use case?"

In this case I would recommend thinking about the amount of data used in your dropdowns. If you have only few elements you could have them all in your page and just use javascript to show the appropiate ones. A second option could be using a REST call with JSON as respresentation because it's easy to use with javascript and very lightweight which of course would also mean doing AJAX.

2
  • When i use Ajax to load a portion of the webpage, that AJAX request is an http call to teh server...more specifically..it calles a specific method on my servlet. is it safe to say that in this case AJAX is using REST?
    – Kaushik
    Jul 25, 2013 at 13:34
  • Well it depends on the method you are calling. If the method e.g fulfills the constraints of a RESTful WebService (en.wikipedia.org/wiki/Representational_state_transfer) you could say so yes.
    – Lesstat
    Jul 26, 2013 at 13:23
4

REST is using HTTP verbs GET, POST, PUT, DELETE to respectively get, insert, update, delete resources on a server. E.g. GET with url: controller/customer/{id}
The alternative is adding methods to a controller such as GetCustomerById(id), InsertCustomer(customer), UpdateCustomer(customer), DeleteCustomer(customer). This is what's known as a RPC approach. E.g. GET with url: controller/GetCustomerById?id={id}
One of the differences is, is that a REST api is kind of predictable. If you know REST and you know the name of the resource you want (e.g. Customer), then you can immediately jump in and use the REST controller with the standard verbs.
With an RPC approach, you need to know what methods are on your controller, what kind of arguments they take, etc. These signatures can vary from controller to controller, from app to app.
AJAX on the other hand is just an asynchronous way of placing the aforementioned requests, whether as a REST call or a RPC call.

1
  • The alternative is still REST. Rest is defined by the set of operations, NOT the protocol used.
    – Jan Hudec
    Jul 25, 2013 at 8:26

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