Reading Server Data

We begin our exploration of Ajax with the basics: using the XMLHttpRequest object to retrieve and display information from a server.

To get the job done, we’ll use Scripts 13.1 (HTML) and 13.2 (JavaScript). There are two possible files that can be read: the XML file that is shown in Scripts 13.3 and the plain text file that is Scripts 13.4.

To request server data:

1.
var xhr = false;
In Script 13.2, the xhr variable is one that you’ll be seeing a lot of in this chapter. It’s an XMLHttpRequest object (or it will be later, after it’s initialized). At this point, we just need to create it outside any functions in order to make it globally available.
2.
function initAll() { document.getElementById("makeTextRequest").onclick = ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.