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
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
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
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