7. AJAX and REST

AJAX is a popular web development technique that makes web pages interactive using JavaScript.

In AJAX, requests are sent to the server using XMLHttpRequest objects. The response is used by the JavaScript code to dynamically change the current page.

In many ways, AJAX applications follow the REST design principles. Each XMLHttpRequest can be viewed as a REST service request, sent using GET. And the response is often in JSON, a popular response format for REST. (See REST Server Responses, above.)

To make your AJAX application truly RESTful, follow the standard REST design principles (discussed later). You will find that most of them contribute to a good design, even if you don't think of your architecture in terms of REST.

A later section provides code samples for issuing HTTP requests in JavaScript, but if you've done any AJAX programming, you are already familiar with all that.

3 comments:

GG said...

Hi, I have a comment on the line above that says .... each ajax XMLHttpRequest sent as GET. I was curios why you restricted it just to GET? as POST or PUT ... are also possible. You made it sound like only GET requests.

Thanks for keeping this blog.

Gg

Guy said...

Hello, I am wondering why you said that JSON is the most popular response format for REST but you also said that "A server response in REST is often an XML file"? Sorry, I'm confused.

Dr. M. Elkstein said...

Hello Guy,

It's actually hard to measure what is more "common". JSON is very popular nowadays, as XML is deemed "heavyweight" and cumbersome, and client-side processing is often done in JavaScript (where JSON is a native format). But both output formats are in heavy use in the industry. You can use either -- or other formats as well; whichever fits your application best. REST, as an architecture, does not dictate one format or the other.