JavaScript xmlHttpRequest.Send() issues.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: xmlHttpRequest.Send() issues.

Threaded View

  1. #1
    Join Date
    May 2012
    Posts
    8

    xmlHttpRequest.Send() issues.

    I have the following code in Javascript to talk to my Comet server:

    Code:
                function getXmlHttpRequestObject() {
                    if (window.XMLHttpRequest) {
                        //document.getElementById("_receivedMsgLabel").innerHTML += "Non-microsoft xmlHttpRequest object created.<br/>";
                        alert("Non-microsoft xmlHttpRequest object created.");
                        return new XMLHttpRequest();
                    }
                    else if (window.ActiveXObject) {
                        //document.getElementById("_receivedMsgLabel").innerHTML += "Microsoft xmlHttpRequest object created.<br/>";
                        alert("Microsoft xmlHttpRequest object created.");
                        return new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    else {
                        alert("Status: Could not create XmlHttpRequest Object.  Consider upgrading your browser.");
                        //document.getElementById("_receivedMsgLabel").innerHTML += "Status: Could not create XmlHttpRequest Object.  Consider upgrading your browser.<br/>";
                    }
                }
    
                var sendReq = getXmlHttpRequestObject();
                var receiveReq = getXmlHttpRequestObject();
                var lastMessage;
                var mTimer;
    
                //Gets the server response:
                function getResponse() {
                    document.getElementById("_receivedMsgLabel").innerHTML += "getResponse() called.<br/>";
                    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
                        //if (receiveReq.readyState == 0) {
                        //receiveReq.open("POST", 'http://tardis:1802', true, "server", "server123");
                        //receiveReq.open("POST", 'http://localhost:1802', true, "server", "server123");
                        //receiveReq.open("POST", 'http://holit109:1802', true, "server", "server123");
                        //receiveReq.open("POST", "http://localhost:1802", true, "server", "server123");
                        receiveReq.open("POST", "http://L45723:1802", true, "server", "server123");  //must use this URL at work.
                        receiveReq.onreadystatechange = handleReceiveMessage;
                        alert("handleReceiveMessage assigned to onreadystatechange event.");
                        receiveReq.setRequestHeader("Content-Type", "text/x-json");
                        receiveReq.timeout = 0;
                        var currentDate = new Date();
                        var sendMessage = JSON.stringify({
                            SendTimestamp: currentDate,
                            Message: "Message 1",
                            Browser: navigator.appName
                        });
                        //receiveReq.send("<?xml version='1.0'?><Request><Command>Queue</Command><User>user1</User><Message>Message 1</Message><Message>Message 2</Message><Message>Message 3</Message></Request>");
                        alert("JSON message created.  About to send...");
                        receiveReq.send(sendMessage);
                        alert("Message sent.");
    
                    }
                }
    
                //function for handling the return message from Comet
                function handleReceiveMessage() {
                    if (receiveReq.readyState == 4) {
                        document.getElementById("_receivedMsgLabel").innerHTML += "Message received!<br/>";
                        var status = receiveReq.status;
                        //document.getElementById("_receivedMsgLabel").innerHTML += "Status received!<br/>";
                        var txt = receiveReq.responseText;
                        var receivedMsg = JSON.parse(txt);
                        document.getElementById("_receivedMsgLabel").innerHTML += receivedMsg.Message + "<br/>";
                        //var receivedTime = new Date();
                        //alert("Got current date.");
                        //alert(receivedTime);
                        //var receivedTime_ms = receivedTime.getTime();
                        //alert("Got time message received in ms.");
                        //alert(receiveReq.SendTimestamp);
                        //var sentTime_ms = getDateFromFormat(receiveReq.SendTimestamp, "dd/MM/yyyy HH:mm:ss");
                        //var sentTime = new Date(receiveReq.SendTimestamp);
                        //alert("Got time message sent in ms.");
                        //var sentTime_ms = sentTime.getTime();
                        //var difference_ms = receivedTime_ms - sentTime_ms;
                        //document.getElementById("_receivedMsgLabel").innerHTML += "Comet took " + difference_ms + " ms.<br/>";
    
                        mTimer = setTimeout("getResponse();", 0);
                    }
    
                    getResponse();
                }
    The xmlHttpRequest.Send() won't run in Firefox. It crashes there.
    In Safari, Opera and Chrome, it runs.
    In Safari the handleReceiveMessage() is never called.
    In Opera and Chrome the handleReceiveMessage() is called, but nothing is returned in the xmlHttpRequest.responseText property.

    This only works in IE.

    What am I doing wrong?
    Last edited by PeejAvery; May 30th, 2012 at 09:34 AM. Reason: Added code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center