jump to navigation

jinzai-studio.net Developer’s Weblog September 6, 2012

Posted by jinzaistudio in ASP.NET 3.5, Javascript, jinzai-studio.net, programming, Visual Basic, Visual C#.
add a comment

Here is a link to my development journal for the site:
jinzai studio Developer’s Weblog
In the blog, I cover programming topics related to ASP.NET web site creation, such as XML, javascript, SQL access, web services, asynchronous programming, security and many other subjects.

Advertisements

Javascript For Creating Client Requests in ASP.NET April 5, 2012

Posted by jinzaistudio in ASP.NET 3.5, Javascript, jinzai-studio.net, Michael T. Blake, programming.
add a comment

Here is the client side Javascript that runs in the client’s browser. ASP.NET stands for Active Server Pages, which are naturally server side entities. Most of the interactive part of a web page starts with the user doing something in their browser….that is, on the client side. As I have stated before, the interaction creates an action back to the server and a response to the client. In order to avoid too much of that, there is an ability on the client side to handle some things within the browser. Since this is not a consistent paradigm, a platform independent way to code is necessary. Javascript to the rescue. The interaction between Javascript running on the client and .NET code running on the server presents many challenges to the programmer, not the least of which is security. Its a fairly large hole in the system which is in constant need of plugging up. Here is some basic Javascript code for creating a BOSH session request, which is the first step of many in this project. I will not bore you with it much longer here…it will be available to use and examine on the website once I get the core functionality working to my satisfaction.

I should note that the code to instantiate the clientRequest was found using a Google search. I was willing to just support the current XMLHttpRequest in IE8 and above, but I do like backward compatibility.

    
        var clientRequest;

        function readyStateChangeHandler() {
            if (clientRequest.readyState == 4) {
                if (clientRequest.status == 200) {
//                    alert(clientRequest.responseText);
                    document.getElementById("divResponse").innerHTML += 
clientRequest.responseText;
                }
            }
        }

        function StartSession() {
            clientRequest = new ajaxRequest();
            if (clientRequest != null) {
                var fromText = document.getElementById("idFrom").value;
                var holdText = document.getElementById("idHold").value;
                var ridText = document.getElementById("idRid").value;
                var toText = document.getElementById("idTo").value;
                var routeText = document.getElementById("idRoute").value;
                var verText = document.getElementById("idVer").value;
                var waitText = document.getElementById("idWait").value;

                if (fromText.length > 0 &&
                    holdText.length > 0 &&
                    ridText.length > 0 &&
                    toText.length > 0 &&
                    routeText.length > 0 &&
                    verText.length > 0 &&
                    waitText.length > 0) {
                    
                    var theSessionrequest = "";
                    alert(theSessionrequest);
                    clientRequest.open("POST", 
"http://localhost:4491/jinzai-studio.net/ChatterBox.aspx", 
true);
                    clientRequest.setRequestHeader("Content-Type",
 "text/xml; 
 charset=utf-8")
                    clientRequest.onreadystatechange = readyStateChangeHandler;
                    //                    clientRequest.send("");
                    clientRequest.send(theSessionrequest);
                }
            }
        }

        function ajaxRequest() {
            var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];

            if (window.ActiveXObject) {
                for (var i = 0; i < activexmodes.length; i++) {
                    try {
                        return new ActiveXObject(activexmodes[i]);
                    }
                    catch (e) {
                        //suppress error
                    }
                }
            }
            else if (window.XMLHttpRequest) {
                return new XMLHttpRequest();
            }
            else {
                return false;
            }
        }

        function SetHiddenFieldValue(theField, theValue) {
            document.getElementById(theField).value = theValue;
        }

        function Send(theText) {
            alert(theText);
        }