![]() |
Web Development »
ASP.NET »
General
Beginner
AJAX-A New approach to build better web-applicationBy ChiranjibCAn article on AJAX |
Windows, .NET, ASP.NET, Visual Studio, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Asynchronous JavaScript And XML, or its acronym, Ajax, is a Web development technique for creating interactive web applications. It is used to submit a server request from a client using JavaScript and XML, without submitting the whole page. As a result the whole process is exchanging small amount of data, therefore is much faster and more responsive as a client-server based system. A nice implementation of AJAX can be seen in http://www.google.com/webhp?complete=1&hl=en. If you write any alphabet into the textbox a dropdown appears, which is coming directly from the server, without submitting the whole page. The heart of AJAX is XMLHttpRequest object. The client can retrieve and submit the XML data directly in the background. To convert retrieved XML data into renderable HTML content, rely on the client-side Document Object Model (DOM) to read the XML document node tree and compose HTML elements that the user sees. AJAX is not a single technology like HTML, DHTML etc. It is the combination of different technologies:
� The XMLHttpRequest object is used to exchange data asynchronously with the web-server.
� XML is commonly used as the format for transferring data back from the server, though any format could be used, like Plain Text, HTML etc.
� If XML is used as the transferring format then DOM is usually used with a client-side scripting language like JavaScript to dynamically display and interact with the information presented.
� XHTML (or HTML), CSS, for marking up and styling information.
var req = new XMLHttpRequest();
For the Internet Explorer 5.0 and above, pass the name of the object to the ActiveX constructor: var req = new ActiveXObject("Microsoft.XMLHTTP");The methods of the objects control all operations, while its properties hold, among other things, various data pieces returned from the server, like xmlHttpObject.responseText contains the returned xml or string values from the server.
| Methods | Descriptions |
|---|---|
| abort() | Abort the current request. If you call it on an object which isn�t processing a request (readyState 0 or 4) - weird things happen. |
| getResponseHeader("headerLabel") | Returns the string value of a single header label |
| getAllResponseHeaders() | Returns complete set of headers (labels and values) as a string |
| open("method", "URL"[, asyncFlag[, "userName"[, "password"]]]) | Assigns destination URL, method, and other optional attributes of a pending request |
| send(content) | Transmits the request, optionally with postable string or DOM object data |
| setRequestHeader("label", "value") | Assigns a label/value pair to the header to be sent with a request |
var req; ��������� req = new ActiveXObject("Microsoft.XMLHTTP"); ����� var url = "AjaxServer.aspx?PubID="+ID; ����� //Open a GET request to the URL req.open("GET", url, true); //Actually Sends the request with a null body. req.send(null);
| ***In the sample application AjaxClient.aspx page is the user-interface and the AjaxServer.aspx is catering the data as per the user request. One important thing to note is AjaxServer.aspx page should not contain any HTML. You can test what will happen if that page contains HTML. |
[ //Actually Sends the request with a null body. req.send(null); ]
| Property | Description |
|---|---|
| onreadystatechange | Event handler for an event that fires at every state change readyState Object status integer: 0 = uninitialized 1 = loading 2 = loaded 3 = interactive 4 = complete |
| responseText | String version of data returned from server process |
| responseXML | DOM-compatible document object of data returned from server process |
| status | Numeric code returned by server, such as 404 for "Not Found" or 200 for "OK" |
| statusText | String message accompanying the status code |
//This is the event handler mechanism; In this //case "RequestProcessor" is the event handler. req.onreadystatechange = RequestProcessor;For this application �RequestProcessor� is the event-handler for the client side. Now inside the event-handler (�RequestProcessor�) use readyState property to get various states. The value 4 is used to indicate the completion of certain processing. Now before processing the result one should check the status or statusText to determine the success or failure of the operation. In this application I did it in the following way:
function RequestProcessor()
{
//If the readyState is in the "Ready" state
if(req.readyState == 4)
{
//Returned status code 200 means Everything is fine.
if(req.status == 200)
{
//If responseText is not blank
//req.responseText is actually a string written by AJAXServer.aspx by its
//"Response.Write("" + sbXML.ToString() + "");" method.
if(req.responseText != "")
{
populateList(req.responseXML);
}
else
{
clearSelect(publishedBooks);
}
}
}
return true;
}
****The object req is declared as page level variable as:
var req = new ActiveXObject("Microsoft.XMLHTTP");
The url of the request destination must be under the same domain as the client script belongs. The reason is XMLHttpRequest object adopts the same �Sandbox� as it were. On most browsers supporting this functionality, the page that bears scripts accessing the object needs to be retrieved via http: protocol, meaning that you won't be able to test the pages from a local hard disk (file: protocol).
function callInProgress(xmlhttp) {
switch ( xmlhttp.readyState ) {
case 1, 2, 3:
return true;
break;
// Case 4 and 0
default:
return false;
break;
}
}
Now before calling send(), I can look first to see if the object is busy e.g. if ( !callInProgress(xmlhttp) ) { xmlhttp.send(null); } else { alert("I'm busy. Wait a moment"); }
<APPSETTINGS><ADD value="data source=cal-slcu2-196;Database=pubs;User=sa;PWD=sa" key="CONN_STRING" /></APPSETTINGS>
Variable or class names should be wrapped in <code> tags like this.
Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?
Thanks to www.ajaxblog.com which helped me to write this article and few previous ajax-articles in codeproject which helped me to build the basic understanding on AJAX.
Keywords for making search easier through google (NOT at all linked with this artticle, just skip this portion):
AJAX, XMLHttpRequest, Dynamic Web Application, Programming, ASP .Net, ASP .Net 2.0, ASP .Net2.0, C#, Building AJAX application, Java Script
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 9 Jul 2006 Editor: |
Copyright 2006 by ChiranjibC Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |