Every time you visit a Web page URL (e.g. - www.codylindley.com) you are making an HTTP (Hypertext Transfer Protocol) request to a server. The response to the request is the page that loads in the browser. Open a browser, go to codylindley.com, and there you have it -- you made an HTTP request and got an HTTP response (shameless self promotion). This is the Web paradigm that most Web developers are familiar with, regardless of whether or not you know all the fine details of HTTP.
AJAX, when you boil it down, is about manually making "micro" HTTP requests using the XMLHttpRequest (XHR) JavaScript object. It mimics the behavior of the user agent, but instead of using the browser interface, you are using JavaScript to make a programmatic HTTP request (which also handles the response). This technique actually has many names: AJAX, scripting HTTP, XHR, or remote scripting, but personally I prefer the term XHR.
The nuts and bolts of XHR are rather simple when compared to the workings of browsers like Firefox or Internet Explorer. Truthfully, the basic HTTP life cycle is the same. What is not the same, is that the browser was not designed for this type of hidden (hidden from the user) micro request and response. The page refresh is the most common indicator that something is going to happen, and XHR eliminates it altogether. So, XHR from the browser's perspective is really quite a hack. This is where our hardships begin.
I have put together this comprehensive guide in order to help you get a better foothold on XHR, and to help diminish some of these hardships. The following is a long list of concepts surrounding the use of the XMLHttpRequest JavaScript object for the purpose of handling HTTP requests and responses.
Remote scripting techniques pre-date XHR
Former solutions, some actually still in use today, include:
- Applet, ActiveX control, Flash
- XML-RPC
- Simple HTTP via a hidden iframe
Each of these techniques has advantages and disadvantages, but more than anything, each was a sign that the needs of Web developers were not being adequately met by browser makers. This is the primary reason XHR came into existence.
Multi-page architecture (Web 1.0) vs. single-page architecture (Web 2.0)
The traditional web, or web 1.0, facilitates the default functionality of the Web browser. When you request a page, the browser refreshes, and a new page is displayed. This is the intended functionality of a Web browser. XHR works outside of the browser model. Single-page architecture, or Web 2.0, uses micro HTTP requests to update the page without a page reload, and really without the browser's knowledge. Web 2.0 is about a lot of things (social networking, tagging, community, etc.), but in my opinion it all boils down to making micro HTTP requests outside of the traditional HTTP model used by Web browsers. This is helping to bring the desktop to the Web.
Why develop using a single-page architecture?
- XHR allows us to build more desktop-like applications in the web browser.
- XHR avoids a complete page reload when only a small portion of the UI needs to change, which allows for immediate access to server-side logic and data.
- By updating chunks of an interface on demand, the application can inform the user immediately (e.g - live validation or live searches).
- If the request stalls, the user can perform other tasks while waiting, unlike the multi-page architecture.
- Page size can be managed by triggering the addition of content when needed, and not on initial page load.
Why build browser based applications?
- There is nothing to install on the user's computer.
- The data hosted on a network is available from anywhere.
- The Web is OS-agnostic, so it has a larger reach.
- Upgrades can happen without user intervention.
- Most users already have what they need -- a Web browser!
Why use XHR and JavaScript instead of Flash, Applets, Iframes, etc.?
- Iframes are a hindrance to SEO and accessibility.
- No third-party plugins are required.
- It is the simplest of all the remote scripting techniques.
- XHR is universally much easier to support given the popularity of Web 2.0 applications.
But wait, Remote Scripting breaks the browser
Rich Internet Applications (RIAs) using XHR should be built specifically with the Web browser in mind, since the browser and the OS are not created equal. Developers must take into account the limitations of this unique delivery mechanism when planning to build a Web site or Web application. In reality, remembering this concept is crucial during all phases of the project -- the visual design, interaction design, and technical design -- even quality assurance.
RIAs that implement remote scripting techniques break the traditional model that browsers support. This effects the user's expectations and experience. Specifically, XHR breaks the history log, back button, bookmarking, and the URL itself. Applications using XHR or some flavor of remote scripting will require unique URLs to access specific states of the application. Think Google maps -- they do this by providing a link to a specific map. In a traditional page model this is not a problem because the URL shown in the browser and managed by the browser represents the exact state of the page. Some of these hurdles have been overcome, but not without a heavy reliance on JavaScript.
- History -- Because the browser will not monitor micro HTTP requests and responses, they are not accounted for in the browser history log.
- Back Button -- Because there is no history log for XHR, the back button will only return to URLs that the browser manages. If you are using both a traditional page architecture and single page architecture together, the back button will work differently depending upon what is managing the HTTP.
- URL -- The URL becomes broken when using XHR because the URL no longer represents a single state of an interface. The response of the URL that appears in the browser no longer represents what you will actually see.
Visit the following links for more information on fixing a few of these behaviors:
Because remote scripting breaks the browser, it also breaks SEO
In simple terms, if a URL no longer represents a search-able and bookmark-able page, then search engines will have trouble indexing the content. Remember, search engines do not care about JavaScript, other than the fact that it can become a stumbling block. This means that there is great potential that your search ranking will suffer. Web developers should become familiar with the means by which this can be overcome, as well as communicating the pitfalls to clients.
The client/server relationship (tight coupling vs. loose coupling)
How dependent (intertwined) is the client on the server when using XHR? In tight coupling the client is dependent on the server in a very specific manner, whereas in a loose coupling the handshake between the server and the client only cares about the format of the request, and the structure of the returned data. Most web services are good examples of loose coupling.
The JavaScript / interface relationship (tight coupling vs. loose coupling)
How dependent (intertwined) is the interface on the javascript? Loose coupling typically involves using a generic library of JavaScript code to create your own AJAX enabled interfaces -- interfaces that you build from the ground up. Tight coupling here means that the JavaScript is intertwined with the interface or the server, or both. Any time the server-side logic or JavaScript is creating the interface (XHTML and CSS), you are dealing with tight coupling. This can have consequences if you are concerned about accessibility.
Web 2.0 Accessibility
Building accessible web pages in the traditional Web world is difficult; building accessible Web 2.0 Web pages is even more difficult. The bottom line is that true accessibility can be expensive and time consuming, though this varies greatly depending on the level of access you want to provide to your visitors. If it is an all-encompassing audience, then you must spend even more money and time. If you have to build an accessible Web 2.0 application, then you will need to first build an accessible Web 1.0 application, and then enhance it (via progressive enhancement) with remote scripting. In my opinion, "graceful degradation" is more of an afterthought than a real approach to the accessibility issue as it pertains to XHR. The best approach is Hijax, first introduced by Jeremy Keith.
Typical browser support for XHR development and rich interfaces
- IE 6.x on Windows 2000, XP, and Vista
- IE 7 on Windows XP and Vista
- Firefox 1.5+ on Windows and Mac
- Camino 1.x on Mac
- Safari 2.x on Mac
- Safari 3.x on Windows XP, Vista and Mac
- Opera 9.x on Windows and Mac
RESTful (Representational State Transfer) Web service principles
Simply stated, REST is an architecture style. In practice, when you hear the term REST, it typically refers to a service call on the Web that you connect with using a unique URL (sometimes including an API key). This call returns a stateless chunk of data in XML, JSON, or text format. The REST call is "dialed" with XHR.
Example:
You can almost read this URL and know what you are going to get back. We can discern from the URL that:
- It is a Web search service from Yahoo!.
- It is version 1 of this REST architecture.
- I am requesting the top 2 results of a Web search using the keyword "madonna".
JSONP
JSON with Padding (JSONP) is a concept that allows the sender of an HTTP request (On-demand Script Tag), where JSON is returned, to provide a name for a function that is invoked with the JSON object as a parameter of the function. This technique does not use XHR, and is an alternative approach that has gained some traction among developers.
Example: