HTML and CSS Reference
In-Depth Information
Consuming JSON and XML data by using web services
The two data formats commonly used in data transmission are JSON and XML. JSON is
unstructured data, whereas XML is structured. JSON uses a special syntax that allows the
definition of name value pairs in a lightweight string format. XML, as a relative of HTML, is
more structured than JSON with named tags and opening and closing tags. Tags can have
attributes. The following are examples of what a person object might look like in both for-
mats where the person object has a first name, last name, hair color, and eye color:
{firstName: "Rick", lastName: "Delorme", hairColor: "brown", eyeColor: "brown" }
XML (Elements):
XML (attributes):
<Person firstname="Rick" lastName="Delorme" hairColor="Brown" eyeColor="Brown"/>
When publishing data services such as Web Services or a REST API, you can control how
you publish the data. When consuming third-party resources, you won't have control over
how they've published the data.
Using the XMLHttpRequest object
JavaScript provides built-in support for receiving HTML data via the XMLHttpRequest ob-
ject. The object makes a call to a web service, REST API, or other data provider services. The
advantage of doing this via JavaScript on the client side is to be able to reload portions of the
page from an external source without having to post the entire page back to the server.
XMLHttpRequest makes an HTTP request and expects to receive back data in XML format.
Both synchronous and asynchronous calls are supported. Table 3-3, Table 3-4, and Table 3-5
list the available events, methods, and properties of the XMLHttpRequest object.
TABLE 3-3 Available events of the XMLHttpRequest object
Sets an event handler for when the state of the request has changed. Used
for asynchronous calls.
Sets an event handler for when the request can't be completed.
Search WWH ::

Custom Search