live wid knowledge :)

Synchornous communication of client (browser) and server (IIS)

To make communication between client and server, we use XMLHTTPRequest object, same object used in AJAX, CALLBACK techniques and where ever we have to send request to server from client (browser).

XMLHTTPRequest communicate with server asynchronously, means invoker/caller doesn't wait for completion of completion of that function/subroutine/method, which it called so proceed but what if we have DEPENDENCY on result of the method/function which we invoked through XMLHTTPRequest object and suppose to proceed only when we get result and on the base of that result, we have to code further like:

Suppose i need to make functionality of SIGNIN  means Authenticate User. User enter his/her username and password and press Sign In and on SignIn we call javascript function which call server side function through XMLHTTPRequest object and that server side function check either user exist in DB or not if Yes then return True if no then return False and return that result back to client side and if True then redirect to WelcomePage if False then take to Error Page. Here you can see until we don't get result (either True or False).

so the solution is following code/snippet which you need to have along your code :)

<script type="text/javascript">
    var __xmlHttpRequest = window.XMLHttpRequest;
    window.XMLHttpRequest = XMLHttpRequest = function() {
        var _xmlHttp = null;
        if (!__xmlHttpRequest) {
            try {
                _xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(ex) {}
        }
        else {
            _xmlHttp = new __xmlHttpRequest();
        }
       
        if (!_xmlHttp) return null;
        
        this.abort = function() {return _xmlHttp.abort();}
        this.getAllResponseHeaders = function() {return _xmlHttp.getAllResponseHeaders();}
        this.getResponseHeader = function(header) {return _xmlHttp.getResponseHeader(header);}
        this.open = function(method, url, async, user, password) {
            return _xmlHttp.open(method, url, false, user, password);
        }
        this.send = function(body) {
            _xmlHttp.send(body);
            this.readyState = _xmlHttp.readyState;
            this.responseBody = _xmlHttp.responseBody;
            this.responseStream = _xmlHttp.responseStream;
            this.responseText = _xmlHttp.responseText;
            this.onreadystatechange();
        }
        this.setRequestHeader = function(name, value) {return _xmlHttp.setRequestHeader(name, value);}
    }
</script>

 

It will work either you are using XMLHTTPRequest or AJAX or even ASP.NET CALLBACK butttttttttt in case of ASP.NET CALLBACK you can avoid that snippet,  when you register/embed client side method signature in "GetCallbackEventReference"

String cbReference = scriptMgr.GetCallbackEventReference(this"arg", "ReceiveServerData", "", TRUE|FALSE);
TRUE for Async and FALSE for Sync communication. 
ref: http://aspalliance.com/1573_Working_with_Callback_and_Control_Rendering.all listing 3

 

Comments

Alex said:

This works great in AJAX 2.0 but DOES NOT work in AJAX 3.5.  How can we get this working in MS AJAX 3.5 ??  Thanks.

# July 30, 2008 4:26 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)