Canna
Canna

Reputation: 3804

Confused with RESTful service and ajax

I'm not a good english speaker, give me mercy about the bad explanation

As developing web, i am always confused about the RESTful's definition.

OK, for instance, the client(i.e browser, mobile or etc) wants a user list

so the client request a GET method url.

domain/give_me_user_list

gotcha! the server responses a json format data like this.

{
    "users": [
        {
            "name": "john doe"
        },
        {
            "name": "john smith"
        }
    ]
}

Is this RESTful service? that's all?

But what about ajax call?

$.ajax ->
 url: domain/give_me_user_list,
 success: (users) ->
  #do somthing

Do we call this RESTful service too?

My question is, RESTful service term sounds like a trendy skill made new, but it my feeling it is just an ajax call...

Upvotes: 1

Views: 53

Answers (1)

Andrew Miner
Andrew Miner

Reputation: 6165

They're really two completely different things which are frequently used together.

RESTful Services

RESTful describes a way for a service to define its API. It favors having a single URL per type of request, and using the native action methods built into HTTP. Contrast this with a another common pattern where all requests go to the same URL using the POST method, and the description of the desired resource and action are encoded within the message body.

So, let's consider an example of each approach. Let's say we have a typical blog which is made up of posts with related comments. Using the older non-RESTful style, you might make the following request to read a specific blog post, and then to write some change:

POST /service/action.do
    body: { "action":"read", "resource":"blog_post", "id":1234 }

POST /service/action.do
    body: {"action":"update", "resource":"blog_post", "id":1234, text:"Lorem ipsum..." }

With a RESTful service, it would probably look something more like:

GET /service/blog_post/1234
    body: none

PUT /service/blog_post/1234
    body: { "text":"Lorem ipsum..."}

There's lots more to it, and some of it pretty subtle, but that's the main difference between a RESTful and non-RESTful service API.

AJAX

AJAX describes an approach to building web pages where new content can be added to the web page without needing to completely refresh it from the server. A small example of this is a finance page which updates the stock price without a page refresh, and a large example is something like Google Maps where you see no full-page refreshes at all.

In both cases, as the user interacts with the page, new content is loaded by using the XMLHTTPRequest object provided as part of the JavaScript environment in the browser. This allows the JavaScript code to make regular HTTP requests without triggering the browser to refresh the page. Once a response is returned, more JavaScript code is used to integrate the new data into the existing page.

Summary

As you can see, the two refer to different things altogether, but can readily be used together to make a dynamic website. The AJAX part refers to how the client is put together, while the RESTful part refers to how the server is put together.

See also: AJAX, RESTful

Upvotes: 2

Related Questions