HTML and CSS Reference
In-Depth Information
EXPLANATION
(
CONTINUED
)
3
The address property also has nested property/value pairs. To get the value of the
state, you would say
car.owner.address.state
.
4
JSON structure must be syntactically correct or the JavaScript
eval()
will not be
able to evaluate properly, all strings quoted, curly braces lined up. It appears that
if you want to create an array of hashes, the syntax requires an object notation
rather than array notation; that is, use curly braces on the outside of the whole
structure, not [ ].
Ajax Program with JSON and
eval()
EXAMPLE
18.21
<html>
<head><title>Reading from an JSON file</title>
<script type="text/javascript">
function makeRequest()
{
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function() {
getXMLContents(httpRequest); };
httpRequest.open('GET', "file:///C:/wamp/www/ajaxCar.json",
true);
/*
Check that you use the correct URL for your browser. IE
wanted “http://localhost/ajaxCar.json”. Firefox complained
about the a not well-formed .json file
*/