Ajax: Creating Native JavaScript Objects From SQL
(Page 1 of 4 )
Ajax is useful for fetching data to web pages without requiring that the pages reload; it speeds up the user experience of your web site. This article will show you how to build an ASP.NET web application in C# that takes advantage of Ajax's dynamic ability.
A
downloadable zip file is available for this article.
Overview
Ajax stands for Asynchronous JavaScript and XML. It's an important technology because it allows you to fetch data after a web page has already been loaded, and modify the page according to the dynamic data it receives "behind the scenes." As the name implies, Ajax was initially intended for fetching XML. Once the XML data is received, the JavaScript code in the web page has to parse it and do something with it that your browser can understand. Usually, this means converting it into HTML, or modifying existing HTML form field values. But Ajax is not restricted to getting XML data from the servers.
JSON stands for JavaScript Object Notation. It's a lightweight data-interchange format that the JavaScript language can understand natively, as an object. So, unlike XML, which has to be parsed, the JSON data can be converted directly into a real JavaScript object with a single call to the eval() function. You can then access the data the way you would any other JavaScript object, through member references. Data returned in JSON format is also generally smaller than the equivalent XML representation, which can save bandwidth. It is also "easy for humans to read and write.... [and] ... easy for machines to parse and generate."
In this tutorial I will build a simple ASP.NET web application in C# that will provide a dynamic page (response.aspx) that can be passed SQL statements directly. The page will query against the Northwind database in Microsoft SQL Server, and return the data as a simple JSON object. An HTML document (ajaxtest.html) acts as the "client" to response.aspx, and demonstrates Ajax methods using JSON data.
Next: Setting up the .NET project >>
More ASP.NET Articles
More By James Robson