Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts

Tuesday, June 5, 2007

Reverse Ajax

A couple of months back, i had posted simple AJAX tutorials on how to initiate simple AJAX calls and get the response from the server. Today I stumbled upon an article on reverse AJAX, click here to view the original post.

Reverse AJAX is actually an implementation (seems more of a marketing buzz word to me) rather than a technology. In case of AJAX it is the browser that initiates the request, but what if the server needs to inform the browser of something ?

Suppose there are 6 clients (browsers) connected to a server viewing the same financial record, if one of the clients updates the record then how do the other connected clients get informed that the record has been updated ? this is a typical problem that a reverse AJAX solution can be used to target.

There are generally 3 methods that can be used for implementation of Reverse AJAX, the details can be read on the original link that I posted earlier

1) COMET or SLOW LOAD
  • COMET was coined by Alex Russell , its a technique to keep keep alive HTTP connections
  • Bryce Nesbitt's example on slow loading, it uses an frames to keep the connection open
2) Polling
  • the browser polls the server after some specified intervals to check if something new has happened
3) PiggyBack
  • piggy backing means that whenever a browser requests an operation, you send back the notification with the result of that request, hence piggy backing on the response
I have not used any of the above mentioned techniques but in my opinion a small framework can be built that sends an asynchronous call to the server and listens for the response, the server would only resonse if some event has occured. This would be similar to slow load technique but at least you wont have to use frames with it

I'll try it out if I can get the time, I'm currently preparing for an MCTS exam due in July... so lets see

Friday, March 30, 2007

Using AJAX

Continuing from the previous post, I'll first post a small tutorial on using ajax note that AJAX is a browser technology, so far I have tested in firefox and IE, ive read somewhere that SAFARI has partial implementation for it, but I didnt check which version was being discussed. first u'll need to create a factory method in your JS file, this method will , depending on the browser, call the correct library and get the ajax object.
it should look something like this:

function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}


You can call the function and get the object that will be sending the actual request

var url = “http://www.sample.comm/sample.asp”;
var xmlObj = GetXmlHttpObject(); //get the object from factory method
xmlObj.onreadystatechange = stateChanged;
xmlObj.open("GET",url,false);
xmlObj.send(null);


a line by line explanation of the code

xmlObj.onreadystatechange = stateChanged;

this line assigns the method to an event, whenever the state of the object changes the method you assign here will be called

xmlObj.open("GET",url,false);

here you are setting the options for opening the connection

arg1 = this can be either “GET” or “POST”, “POST” should be used whenever sending data more than 512kb
arg2 = the url of the server page to hit
arg3 = this parameter specifies whether the call should be sync or async

there are two additional arguments that can be passed, I havnt used them but I think they can be used for HTTP basic authentication

arg4 = username
arg5 = password


xmlObj.send(null);

This is the actual call, note that instead of passing null, you can send xml data in the argument.

Now, whenever the state of the object changes the method that you assigned will be called,

xmlObj.onreadystatechange = stateChanged;

lets look at how we can use this event

function stateChanged(){
if (xmlObj.readyState==4 xmlObj.readyState=="complete") {
alert("success");
}
else{
alert("error");
}
}


The readyState can be any one of the following:

0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete

In this example I have only used the completed state, note that it might be returning its string counterparts depending on the browser

There are other properties which can be used for testing the state of the xml call object
These are:

responseText : get the response as a stirng
responseXML : get the XML DOM object which can then be parsed

status : status number for http such as 200 ok status or 404 error
statusText : get the status as string like “Not Found” or “OK”

from the server side, you can write anything from the page that you have specified in the callback url in Response.Write(“”) , which will become available in the responseText property of the xml obj once the callis complete

similiary , whole forms can be called and retrieved from response.Text, but its not generally a good idea as discussed in the previous post

head over to w3school.com for more tutorials on calling ajax., but this is pretty much there is to it

Thursday, March 29, 2007

Parent child forms in ASP

A month ago, I had to modify an ASP form with some functionality.

this functionality existed in another form, so I decided instead of developing or copy pasting the functionality , i would just call that form as a child in a DIV of the parent form using AJAX.

boy was that a bad decision! believe me it works, the whole form can be called in a div using ajax response.

but i ran into a weird problem that the javascript coming in the AJAX call from the child page didnt run, no matter what. even though the js was there in the AJAX response, I could see it in the debugger

I had to write whole of form validation scripting on the parent page, the elements in the child form were available by using

document.getElementById


in js. I warned another of my colleague who got a similar assignment , he wanted to do it with IFRAME element, and get calls at runtime by changing the form action for the form in the IFRAME element

eventually he ended with similar problems that I was having.

there was another problem with browser caching when using AJAX, when the user updates some values in the child form and submits it and refreshes the parent form, although the changes were reflected in database but the browser (specifically internet explorer) kept showing old values which I believe it keeps picking up from the cache.

theres a browser setting that can be set to check for the new instance of the page on every request, but that didnt seem to work either. We deployed the application with this so called "bug".

I'll post some examples related to this issue later on