Datasheet

Chapter 1: AJAX Technologies
22
{“name”:”someName7”,
“employeeID”:7
}
]
}
]
}
]
}
One of the great things about JSON is that JavaScript provides easy, built-in support for parsing a JSON
representation, as shown in Listing 1-4 . This example is a version of Listing 1-2 that uses JSON.
Listing 1-4: A version of Listing 1-2 that uses JSON
<%@ Page Language=”C#” %>
<%@ Import Namespace=”System.Xml” %>
<%@ Import Namespace=”System.IO” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
void Page_Load(object sender, EventArgs e)
{
if (Request.Headers[“MyCustomHeader”] != null)
{
if (Request.Form[“passwordtbx”] == “password” &&
Request.Form[“usernametbx”] == “username”)
{
string json=”{\”firstname\”: \”Shahram\”,”;
json += “\”lastname\”: \”Khosravi\”,”;
json += “\”employeeid\”: 22223333,”;
json += “\”departmentname\”: \”Some Department\”}”;
Response.Write(json);
Response.End();
}
else
throw new Exception(“Wrong credentials”);
}
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head id=”Head1” runat=”server”>
<title>Untitled Page</title>
<script type=”text/javascript” language=”javascript”>
var request;
if (!window.XMLHttpRequest)
{
// Same as Listing 2
}
function readyStateChangeCallback()
{
if (request.readyState == 4 && request.status == 200)
{
c01.indd 22c01.indd 22 8/20/07 5:40:11 PM8/20/07 5:40:11 PM