Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cannot get result set back from server via ajax

    Hi all!

    I'm working on my ajax stuff, and I'm having an interesting issue.

    The setup:

    #1. I have a server running a servlet on port 8181
    #2. I have Safari loading my test page
    #3. this Test page creates a long term ajax connection to the server
    #4. When the server "sends" data to the client on this line, I can "see" the data on wireshark, and on the browser if I call the server directly. BUT, in my javascript, I do not see anything! It does come back to the callback, but there is no data!!!

    Here is the returning json

    Code:
    /*<event-start>*/{"created":1238535212238,"parent-event-id":"1","parent-event-type":"1000","parent-event-status":"OK","type":4,"id":1}/*<event-end>*/
    The "<event-start>" and "<event-end>" are my delimiters for events.

    Said that, is there anything special on the javascript side?

    Here is now I create the ajax connection

    Code:
        /*
            This method returns the correct http request pipe
        */
        createXMLHttpRequest=function()
        {
            // Lets create the correct type. For the actual hardware, we will not need the Microsoft versions
    
            try { return new XMLHttpRequest();} catch(e)
            {
            }
            try { return new ActiveXObject("Msxml2.XMLHTTP");} catch(e)
            {
            }
            try { return new ActiveXObject("Microsoft.XMLHTTP");} catch(e)
            {
            }
    
            alert("XMLHttpRequest not supported");
    
            return null;
        }
    
        // This method connects to the backend
        this.makeLTEPConnection=function(id, func, url) 
        {
            httpLTEP=createXMLHttpRequest();
            connectionCounter=0;
            connectionErrorCounter=0;
            buffer="";
            
            httpLTEP.open("GET", url, true);    
            httpLTEP.onreadystatechange = func;
            httpLTEP.overrideMimeType('text/plain; charset=x-user-defined');    // we need this else the system will parse the instream as html
            httpLTEP.send(null);
            
            return;
        };
    and here is the callback fun

    Code:
        // This method receives the event message
        this.receivedEventMessage=function()
        {
            try
            {
                var eventResults=extractMPEvents(httpLTEP.responseText);
    
                // skip if we do now have any results
                if(eventResults===undefined)
                {
                    return false;
                }
    .
    .
    .
    Now, I was testing this using not a servlet, but a simple cgi that would emulate the servlet

    Code:
    #!/usr/bin/perl
    $| = 1;
    
    for($i=0;$i<4;$i++)
    {
    
    print "Content-type: text/plain\r\n\r\n";
    
    $value=$i % 4;
    
    print '<event-start>';
    print "{\"event\" :";
    print "    {";
    print "        \"id\" : \"$i\",";
    print "        \"type\" : \"$value\",";
    print "        \"msg\" : \"Kam Was Here!\",";
    print "        \"url\" : \"http://10.5.251.66/demo/screensaver/html/screensaver.html\",";
    print "    }";
    print "}";
    print '<event-end>';
    
    #sleep between 0-1 seconds
    select(undef,undef,undef, rand());
    }
    Use the above script works. When I use the servlet, it sends the data, BUT I do not "see" any data...

    Thoughts?

    ~Kam (^8*

  • #2
    New Coder
    Join Date
    Feb 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi all,

    Okay, the plot thickens!

    All I am doing is calling the "helloworld" servlet in Tomcat using AJAX

    If I call this from the browser, I get everything fine.

    When I use AJAX, I do not...

    Here is the complete ajax code

    Code:
    <html>
    <head>
        <script language="JavaScript">
    
        // working vars
        var http;
    
        function gotReply()
        {
    //        if(http.readyState  == 4)
    //        {
    //            if(http.status  == 200)
    //            {
                    document.getElementById("serverText").innerHTML=http.responseText;
    //            }
    //        }
        }
    
        // This function makes the url call and points to the callback function
        function makeXMLHttpRequest(url, func) 
        {
            try 
            {
                http = new XMLHttpRequest();
            } 
    
            catch (e) 
            {
            }
    
            http.open("GET", url, true);
            http.onreadystatechange = func;
    //        http.overrideMimeType('text/html; charset=x-user-defined');    // we need this else the system will parse the instream as html
            http.send(null);
    
            return;
        }
    
        </script>
    
    </head>
    
    <body id="bodyStart">
    
    <label>
    Version 2.0.3
    </label>
    <br>
    <br>
    <b>
    <label id="info">
    </label>
    </b> <br>
    <br>
    
    <label>
    Enter a URL:
    <!--input type="text" id="url" value="http://10.5.251.66/mp/?service=inevent&payload=%7B%22id%22%3A%221234567%22%2C%22type%22%3A%221001%22%2C%22created%22%3A%221238011621553%22%2C%22message%22%3A%22We%20are%20about%20to%20reload%20a%20document%22%2C%22error-level%22%3A%22ERR%22%2C%22page%22%3A%22Kam%20Test%22%2C%22module%22%3A%22Kam%20Module%22%7D"-->
    <!--input type="text" id="url" value="http://10.5.251.66:/kams/css/examples/tests/callback.sh"-->
    <input type="text" id="url" value="http://10.5.251.66:8080/examples/servlets/servlet/HelloWorldExample">
    <br>
    <br>
    <button onclick="document.getElementById('serverText').innerHTML='Loading Page Data from ['+document.getElementById('url').value+']';makeXMLHttpRequest(document.getElementById('url').value, gotReply)">Press 
    To connect</button> <br>
    <br>
    
    Server Reply <br>
    <br>
    
    <div id="myData">
        Empty!
    </div>
    
    <result id="serverText">
    
    </result>
    
    </body>
    </html>
    Whats interesting is the response header, it shows the correct length, but the data is missing(?) or being consumed...

    The complete Response Header from Tomcat is

    Code:
    Content-Length:359
    Content-Type:text/html
    Date:Wed, 01 Apr 2009 20:43:14 GMT
    Server:Apache-Coyote/1.1
    The "warning" I get in Web Inspector is

    Code:
    Resource interpreted as other but transferred with MIME type text/html.
    Thoughts?

    ~Kam (^8*

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Doh!

    Well, I fixed the issue....

    Ready for this one....?!....

    Well, my "client" script is loaded from port 80, but the ajax makes a call on port 8181! Cannot do that!

    As long as my original html is loaded from port 8181, all is well.

    ~Kam (^8*


  •  

    Posting Permissions

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