Monday, January 26, 2009

This article is for those who are new to Ajax

This article is for those who are new to Ajax. Ajax stands for Asynchronous Java And Xml. Ajax is the great solution for the new age of web applications, it simply helps developer to increase the performance of the site along with the more user interactive UI. Since web page is not getting refreshed each time, it is more users friendly.

The idea is; if page size is 500kb and only 10kb of data is to be updated, then if web application sending and receiving only 10kb data to and from server make more sense, than refreshing whole page. Also the request to server is asynchronously so that user can continue with the work instead of waiting for the response from the server.

It really works fast a very good web experience, in old days of the web development each time user is suppose to wait for the whole page to get refreshed, not a good idea.

Start with AJAX :
Ajax actually works because of the XMLHttpRequest object. Using this object web application can communicate with the server, i.e. either web service or the webpage. In this case web service or webpage will send required data to client page and then on client side using Java script corresponding field can be update.

You can create XMLHttpRequest object as follows:




“request” is the XMLHttpRequest object in above code. (Code is in JavaScript )

Next step after this is to open the object connection and send the request
Open method looks like

XMLHttpRequest.open(method, url, in boolean async, [user, password])
i.e.
request.open('GET', url+parameter, true)
request.send(null)
(Here we are not going to use user and password)
but before this, we need to specify the function which will take care of the data return from the server. i.e. once we send request to the server using XMLHttpRequest object and once response comes from the server which function will handle it.
Before sending the request we will specify the action; for this XMLHttpRequest object posses one event called as onreadystatechange. On this event we can specify which function should be called on each ready state change.
Now before this will understand which are the states XMLHttpRequest object supports.


Ready state valueDescription
0Represents an uninitialized state in which XMLHttpRequest object is created; and not initialized
1Open() method has been successfully called
2Sent () : request has been sent to the server.
3Represent "receiving" state in which the HTTP response headers have been received; message body has not yet been completely received.
4Loaded, data transfer has been completed.


So the above code will change to





Now we will develop a web application in which user will select the value in the combo box and on selection of the value dependent textbox will be filled. In this case on change event of the combo will be used to fire an Ajax request to the server and server in turns will send a response to the client. (Coding is done in asp.net 2.0)

1.Create a web application design a combo box and text box as follows





2.And in code behind add attribute to “InputList” i.e. to combo box.

"callToServer(this.value)" is the javascript function, this function will accept the selected value and send request to the server; using XMLHttpRequest object.

3.Now add one webservice in the same project. Also add following prototypes in web.config file.

Add above code in tab. This is required to communicate with webservice over http.

4.Now write one webmethod in the webservice. i.e.

We will call this webMethod in our Ajax call.

5.After this, will write a JavaScript function "callToServer(this.value)" . This function will be called on onchange event of dropdown list (Ref point 1).

If you observe the url it is the url of the webserivce in the same project.
Here we have to use url which is under the same domain, Firefox doesn’t allow cross refrence, and IE give warning (i.e. IE 6).

Means
var url = "http://domain1/WebSite/webservice.asmx/sendMessage?str="+val;
this will give warning in the IE 6 but Firefox will not allow you to access this webservice.


In the above function check request.onreadystatechange=handleResponse;
Here if you obsever onreadystatechange on function named handleResponse is called. So in the next step we will have to write a function which will handle the response from the server.


6.handleResponse function will be as follows

Since the response is from webservice and it is in the xmlformat, we have to use parser to parse the data. If you want to see what is the response from the server add alert(request.responseText) in the above code after 1st if condition.

window.document.getElementById('DependentOutput').value = data; this line will fill the textbox with the response from the server.


This how we can create Ajax application, it’s very simple and easy. Once you understand this concept, you can modify the code and make it more complex as per your requirements. Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

No comments:

Post a Comment

blogger templates 3 columns | Make Money Online