Prev | Current Page 106 | Next

Larry Ullman

"Building a Web Site with Ajax: Visual QuickProject Guide"

green@thiscompany.com


132 Appendix
Here??™s how that same data would look in JSON format (actually, all the data
would be on one line, without spaces, but I??™ve spaced it out for clarity):
Because JSON data is JavaScript, it??™s arguably easier to work with than
XML. And the same data will likely be smaller in JSON than in XML (meaning a
smaller transfer size from the server to the client). See extra bits on page 146
for why you shouldn??™t use JSON.
[
{???name???:???Gray, Gideon???,???department???:???Accounting???,???emai
l???:???g.gray@thiscompany.com???},
{???name???:???Green, Laverne???,???department???:???Human
Resources???,???email???:???l.green@thiscompany.com???}
]
Appendix 133
sending JSON
Here??™s how the search_results_xml.php script from Chapter 7 would
be rewritten to send JSON data instead of XML:
1 The content-type header indicates what type of data is
being sent by this script.
header(???Content-type: application/json???);
if (!empty($_GET[???last_name??™])) {
require_once(???mysql.inc.php??™);
$q = ???SELECT CONCAT(last_name, ???, ???, first_name),
email, department FROM employees LEFT JOIN departments
USING (department_id) WHERE last_name LIKE ?????? .


Pages:
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118