Google

Saturday, May 24, 2008

Using the XMLHttpRequest Object

As mentioned, the XMLHttpRequest object is the heart of AJAX. This object sends requests
to the server and processes the responses from it. In versions of Internet Explorer prior
to IE7, it is implemented using ActiveX, whereas in other browsers, such as Mozilla
Firefox, Safari, Opera, and Internet Explorer 7, it is a native JavaScript object. Unfortunately,
because of these differences, you need to write JavaScript code that inspects the
browser type and creates an instance of it using the correct technology.
Thankfully, this process is a little simpler than the spaghetti code you may remember
having to write when using JavaScript functions that heavily used DOM, which had to
work across browsers:
var xmlHttp;
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
In this case, the code is simple. If the browser doesn’t support ActiveX objects, the
window.ActiveXObject property will be null, and, therefore, the xmlHttp variable will be set
to a new instance of the native JavaScript XMLHttpRequest object; otherwise, a new
instance of the Microsoft.XMLHTTP ActiveX Object will be created.
Now that you have an XMLHttpRequest object at your beck and call, you can start
playing with its methods and properties. Some of the more common methods you can
use are discussed in the next few paragraphs.
The open method initializes your request by setting up the call to your server. It takes
two required arguments (the Hypertext Transfer Protocol [HTTP] command such as GET,
POST, or PUT, and the URL of the resource you are calling) and three optional arguments
(a boolean indicating whether you want the call to be asynchronous, which defaults to
true, and strings for the username and password if required by the server for security).
It returns void.

Introduction ASP

AJAX is fast becoming a de facto standard for developing responsive and rich web
applications. This evolutionary step in the user experience is being used in more and
more web applications from Outlook Web Access to Google maps and beyond.
But how do you write AJAX applications? Not too long ago, you had to be a JavaScript
expert and use tools that are not as sophisticated as those used in standard ASP.NET
development. As such, it had been difficult and time-consuming to develop, debug, and
maintain AJAX applications despite their innate user friendliness. However, as the popularity
and use of AJAX web applications rose, so did a number of frameworks designed to
ease AJAX development by providing more out-of-the-box functionality. A few of those
packages had been somewhat geared toward developers working with ASP.NET.
After a long beta period, in early 2007, Microsoft officially released the ASP.NET AJAX
Extensions, which include a set of client- and server-side controls and functionality
leveraging some of the existing technologies in ASP.NET. This release also included the
ASP.NET AJAX Toolkit, which contains a set of control extenders that offer enhanced UI
effects and built-in AJAX capabilities that can be used on a page with very little development
effort. With this release, Microsoft brought about major productivity leaps to AJAX
development in the world of ASP.NET.
With ASP.NET AJAX, you can easily convert your existing ASP.NET applications to
AJAX applications, and you can add sophisticated user interface elements such as drag
and drop, networking, and browser compatibility layers, with simple declarative programming
(or, if you prefer to use JavaScript, you can do that too).
This book is a primer on this technology. It introduces you to ASP.NET AJAX, explores
some of the main features and controls, and takes you into how to build AJAX applications
quickly and simply, taking advantage of the IDE productivity offered by Visual
Studio.
It’s going to be a fun ride, and by the end of it, you’ll be an expert in Web 2.0 and
hungry to start developing for it.