HTML and CSS Reference
In-Depth Information
Jason and Ajax Using JSON Library
EXAMPLE 18.24
1 <script type="text/javascript" src="json2.js" >
/* Add the src attribute to your Ajax program to include the
json2.js libaray
Code for creating the XMLHttpRequest object is not included
here. You can find it in Example 18.1.
*/
2 function getXMLContents(httpRequest) {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
textObj=document.getElementById("data");
3
var jasonString = httpRequest.responseText;
alert(jasonString);
// see Figure 18.24.
4
var carObject=JSON.parse(jasonString);
var details="";
5
for(var property in carObject) {
if (property == "owner"){
details += "owner name = " +
carObject[property].name + "<br />";
details += "owner cell phone = " +
carObject[property].cellphone + "<br />";
}
else{
details += property + " = " +
carObject[property] + "<br />";
}
}
textObj.innerHTML=details;
// see Figure 18.25.
}
else {
alert('There was a problem with the request.');
}
}
}
</script>
</head>
<body>
<span style="cursor: pointer; text-decoration: underline"
6
onclick="makeRequest('ajaxCar.json') ">
Get car details
</span>
7
<div id="data">
</div>
</body>
</html>
Search WWH ::




Custom Search